home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gpp-1_42.lha / g++-1.42.0 / cplus-search.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  90KB  |  3,219 lines

  1. /* Breadth-first and depth-first routines for
  2.    searching multiple-inheritance lattice for GNU C++.
  3.    Copyright (C) 1987 Free Software Foundation, Inc.
  4.    Contributed by Michael Tiemann (tiemann@mcc.com)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. /* High-level class interface. */
  24.  
  25. #include "config.h"
  26. #include "tree.h"
  27. #include "cplus-tree.h"
  28. #include "obstack.h"
  29. #include "flags.h"
  30. #include "assert.h"
  31. #include <stdio.h>
  32.  
  33. /* For expand_asm_operands.  */
  34. extern char *input_filename;
  35. extern int lineno;
  36.  
  37. #define NULL 0
  38.  
  39. #define obstack_chunk_alloc xmalloc
  40. #define obstack_chunk_free free
  41.  
  42. extern int xmalloc ();
  43. extern void free ();
  44.  
  45. void init_search ();
  46. extern struct obstack *current_obstack;
  47.  
  48. #include "stack.h"
  49.  
  50. /* Obstack used for remembering decision points of breadth-first.  */
  51. static struct obstack search_obstack;
  52.  
  53. /* Obstack used to bridge from one function context to another.  */
  54. static struct obstack bridge_obstack;
  55.  
  56. /* Methods for pushing and popping objects to and from obstacks.  */
  57.  
  58. struct stack_level *
  59. push_stack_level (obstack, tp, size)
  60.      struct obstack *obstack;
  61.      void *tp;
  62.      int size;
  63. {
  64.   struct stack_level *stack;
  65.   stack = (struct stack_level *) obstack_next_free (obstack);
  66.   obstack_grow (obstack, tp, size);
  67.   obstack_finish (obstack);
  68.   stack->obstack = obstack;
  69.   stack->first = (tree *) obstack_base (obstack);
  70.   stack->limit = obstack_room (obstack) / sizeof (tree *);
  71.   return stack;
  72. }
  73.  
  74. struct stack_level *
  75. pop_stack_level (stack)
  76.      struct stack_level *stack;
  77. {
  78.   struct stack_level *tem = stack;
  79.   struct obstack *obstack = tem->obstack;
  80.   stack = tem->prev;
  81.   obstack_free (obstack, tem);
  82.   return stack;
  83. }
  84.  
  85. #define search_level stack_level
  86. static struct search_level *search_stack;
  87.  
  88. static tree lookup_field_1 ();
  89. static int lookup_fnfields_1 ();
  90.  
  91. /* Allocate a level of searching.  */
  92. static struct search_level *
  93. push_search_level (stack, obstack)
  94.      struct stack_level *stack;
  95.      struct obstack *obstack;
  96. {
  97.   struct search_level tem;
  98.   tem.prev = stack;
  99.  
  100.   return push_stack_level (obstack, &tem, sizeof (tem));
  101. }
  102.  
  103. /* Discard a level of search allocation.  */
  104. #define pop_search_level pop_stack_level
  105.  
  106. /* Search memoization.  */
  107. struct type_level
  108. {
  109.   struct stack_level base;
  110.  
  111.   /* First object allocated in obstack of entries.  */
  112.   char *entries;
  113.  
  114.   /* Number of types memoized in this context.  */
  115.   int len;
  116.  
  117.   /* Type being memoized; save this if we are saving
  118.      memoized contexts.  */
  119.   tree type;
  120. };
  121.  
  122. /* Obstack used for memoizing member and member function lookup.  */
  123.  
  124. static struct obstack type_obstack, type_obstack_entries;
  125. static struct type_level *type_stack;
  126. static tree _vptr_name;
  127.  
  128. /* Make things that look like tree nodes, but allocate them
  129.    on type_obstack_entries.  */
  130. static int my_tree_node_counter;
  131. static tree my_tree_cons (), my_build_string ();
  132.  
  133. extern int flag_memoize_lookups, flag_save_memoized_contexts;
  134.  
  135. /* Variables for gathering statistics.  */
  136. static int my_memoized_entry_counter;
  137. static int memoized_fast_finds[2], memoized_adds[2], memoized_fast_rejects[2];
  138. static int memoized_fields_searched[2];
  139. static int n_fields_searched;
  140. static int n_calls_lookup_field, n_calls_lookup_field_1;
  141. static int n_calls_lookup_fnfields, n_calls_lookup_fnfields_1;
  142. static int n_calls_get_base_type;
  143. static int n_outer_fields_searched;
  144. static int n_contexts_saved;
  145.  
  146. /* Local variables to help save memoization contexts.  */
  147. static tree prev_type_memoized;
  148. static struct type_level *prev_type_stack;
  149.  
  150. /* Allocate a level of type memoziation context.  */
  151. static struct type_level *
  152. push_type_level (stack, obstack)
  153.      struct stack_level *stack;
  154.      struct obstack *obstack;
  155. {
  156.   struct type_level tem;
  157.  
  158.   tem.base.prev = stack;
  159.  
  160.   obstack_finish (&type_obstack_entries);
  161.   tem.entries = (char *) obstack_base (&type_obstack_entries);
  162.   tem.len = 0;
  163.   tem.type = NULL_TREE;
  164.  
  165.   return (struct type_level *)push_stack_level (obstack, &tem, sizeof (tem));
  166. }
  167.  
  168. /* Discard a level of type memoziation context.  */
  169.  
  170. static struct type_level *
  171. pop_type_level (stack)
  172.      struct type_level *stack;
  173. {
  174.   obstack_free (&type_obstack_entries, stack->entries);
  175.   return (struct type_level *)pop_stack_level (stack);
  176. }
  177.  
  178. /* Make something that looks like a TREE_LIST, but
  179.    do it on the type_obstack_entries obstack.  */
  180. static tree
  181. my_tree_cons (purpose, value, chain)
  182.      tree purpose, value, chain;
  183. {
  184.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_list));
  185.   TREE_UID (p) = ++my_tree_node_counter;
  186.   TREE_TYPE (p) = 0;
  187.   ((int *)p)[3] = 0;
  188.   TREE_SET_CODE (p, TREE_LIST);
  189.   TREE_PURPOSE (p) = purpose;
  190.   TREE_VALUE (p) = value;
  191.   TREE_CHAIN (p) = chain;
  192.   return p;
  193. }
  194.  
  195. static tree
  196. my_build_string (str)
  197.      char *str;
  198. {
  199.   tree p = (tree)obstack_alloc (&type_obstack_entries, sizeof (struct tree_string));
  200.   TREE_UID (p) = ++my_tree_node_counter;
  201.   TREE_TYPE (p) = 0;
  202.   ((int *)p)[3] = 0;
  203.   TREE_SET_CODE (p, STRING_CST);
  204.   TREE_STRING_POINTER (p) = str;
  205.   TREE_STRING_LENGTH (p) = strlen (str);
  206.   return p;
  207. }
  208.  
  209. static tree
  210. my_copy_node (node)
  211.      tree node;
  212. {
  213.   struct obstack *ambient_obstack = current_obstack;
  214.   tree t;
  215.  
  216.   current_obstack = &type_obstack_entries;
  217.  
  218.   t = copy_node (node);
  219.   TREE_UID (t) = ++my_tree_node_counter;
  220.  
  221.   current_obstack = ambient_obstack;
  222.   return t;
  223. }
  224.  
  225. /* Memoizing machinery to make searches for multiple inheritance
  226.    reasonably efficient.  */
  227. #define MEMOIZE_HASHSIZE 8
  228. typedef struct memoized_entry
  229. {
  230.   struct memoized_entry *chain;
  231.   int uid;
  232.   tree data_members[MEMOIZE_HASHSIZE];
  233.   tree function_members[MEMOIZE_HASHSIZE];
  234. } *ME;
  235.  
  236. #define MEMOIZED_CHAIN(ENTRY) (((ME)ENTRY)->chain)
  237. #define MEMOIZED_UID(ENTRY) (((ME)ENTRY)->uid)
  238. #define MEMOIZED_FIELDS(ENTRY,INDEX) (((ME)ENTRY)->data_members[INDEX])
  239. #define MEMOIZED_FNFIELDS(ENTRY,INDEX) (((ME)ENTRY)->function_members[INDEX])
  240. #define MEMOIZED_HASH_FN(NODE) (TREE_UID (NODE)&(MEMOIZE_HASHSIZE - 1))
  241.  
  242. static struct memoized_entry *
  243. my_new_memoized_entry (chain)
  244.      struct memoized_entry *chain;
  245. {
  246.   struct memoized_entry *p =
  247.     (struct memoized_entry *)obstack_alloc (&type_obstack_entries,
  248.                         sizeof (struct memoized_entry));
  249.   bzero (p, sizeof (struct memoized_entry));
  250.   MEMOIZED_CHAIN (p) = chain;
  251.   MEMOIZED_UID (p) = ++my_memoized_entry_counter;
  252.   return p;
  253. }
  254.  
  255. /* When a new function or class context is entered, we build
  256.    a table of types which have been searched for members.
  257.    The table is an array (obstack) of types.  When a type is
  258.    entered into the obstack, its CLASSTYPE_MTABLE_ENTRY
  259.    field is set to point to a new record, of type struct memoized_entry.
  260.  
  261.    A non-NULL TREE_TYPE of the entry contains a visibility error message.
  262.  
  263.    The slots for the data members are arrays of tree nodes.
  264.    These tree nodes are lists, with the TREE_PURPOSE
  265.    of this list the known member name, and the TREE_VALUE
  266.    as the FIELD_DECL for the member.
  267.  
  268.    For member functions, the TREE_PURPOSE is again the
  269.    name of the member functions for that class,
  270.    and the TREE_VALUE of the list is a pairs
  271.    whose TREE_PURPOSE is a member functions of this name,
  272.    and whose TREE_VALUE is a list of known argument lists this
  273.    member function has been called with.  The TREE_TYPE of the pair,
  274.    if non-NULL, is an error message to print.  */
  275.  
  276. /* Tell search machinery that we are entering a new context, and
  277.    to update tables appropriately.
  278.  
  279.    TYPE is the type of the context we are entering, which can
  280.    be NULL_TREE if we are not in a class's scope.
  281.  
  282.    USE_OLD, if nonzero tries to use previous context.  */
  283. void
  284. push_memoized_context (type, use_old)
  285.      tree type;
  286.      int use_old;
  287. {
  288.   int len;
  289.   tree *tem;
  290.  
  291.   if (prev_type_stack)
  292.     {
  293.       if (use_old && prev_type_memoized == type)
  294.     {
  295. #ifdef GATHER_STATISTICS
  296.       n_contexts_saved++;
  297. #endif
  298.       type_stack = prev_type_stack;
  299.       prev_type_stack = 0;
  300.  
  301.       tem = &type_stack->base.first[0];
  302.       len = type_stack->len;
  303.       while (len--)
  304.         CLASSTYPE_MTABLE_ENTRY (tem[len*2]) = tem[len*2+1];
  305.       return;
  306.     }
  307.       /* Otherwise, need to pop old stack here.  */
  308.       type_stack = pop_type_level (prev_type_stack);
  309.       prev_type_memoized = 0;
  310.       prev_type_stack = 0;
  311.     }
  312.  
  313.   type_stack = push_type_level (type_stack, &type_obstack);
  314.   type_stack->type = type;
  315. }
  316.  
  317. /* Tell search machinery that we have left a context.
  318.    We do not currently save these contexts for later use.
  319.    If we wanted to, we could not use pop_search_level, since
  320.    poping that level allows the data we have collected to
  321.    be clobbered; a stack of obstacks would be needed.  */
  322. pop_memoized_context (use_old)
  323.      int use_old;
  324. {
  325.   int len;
  326.   tree *tem = &type_stack->base.first[0];
  327.  
  328.   if (! flag_save_memoized_contexts)
  329.     use_old = 0;
  330.   else if (use_old)
  331.     {
  332.       len = type_stack->len;
  333.       while (len--)
  334.     tem[len*2+1] = (tree)CLASSTYPE_MTABLE_ENTRY (tem[len*2]);
  335.  
  336.       prev_type_stack = type_stack;
  337.       prev_type_memoized = type_stack->type;
  338.     }
  339.  
  340.   if (flag_memoize_lookups)
  341.     {
  342.       len = type_stack->len;
  343.       while (len--)
  344.     CLASSTYPE_MTABLE_ENTRY (tem[len*2])
  345.       = MEMOIZED_CHAIN (CLASSTYPE_MTABLE_ENTRY (tem[len*2]));
  346.     }
  347.   if (! use_old)
  348.     type_stack = pop_type_level (type_stack);
  349.   else
  350.     type_stack = (struct type_level *)type_stack->base.prev;
  351. }
  352.  
  353. /* Some simple list processing predicates.  */
  354.  
  355. /* Check whether TYPE is immediately derived from PARENT.
  356.    Return actual base information if so.  Otherwise, return 0.  */
  357. tree
  358. get_base_type_1 (parent, type)
  359.      register tree parent, type;
  360. {
  361.   register int i;
  362.  
  363.   parent = TYPE_MAIN_VARIANT (parent);
  364.   type = TYPE_MAIN_VARIANT (type);
  365.  
  366.   for (i = 1; i <= CLASSTYPE_N_BASECLASSES (type); i++)
  367.     if (TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (type, i)) == parent)
  368.       return CLASSTYPE_BASECLASS (type, i);
  369.  
  370.   return 0;
  371. }
  372.  
  373. /* Check whether TYPE is derived from PARENT.
  374.    Return the actual base information if so.  Otherwise return 0.
  375.    If PROTECT is 1, then emit an error message if access to
  376.    a public field of PARENT would be private.
  377.    If PROTECT is 2, then emit an error message if
  378.    TYPE is derived from PARENT via private visibility rules.
  379.    If PROTECT is 3, then immediately private baseclass is ok,
  380.    but deeper than that, if private, emit error message.  */
  381. tree
  382. get_base_type (parent, type, protect)
  383.      register tree parent, type;
  384. {
  385.   tree xtype = type;
  386.   tree otype;
  387.   int head = 0, tail = 0;
  388.   int is_private = 0;
  389.   tree rval = NULL_TREE;
  390.   int rval_private = 0;
  391.   tree friends = current_class_type
  392.     ? CLASSTYPE_FRIEND_CLASSES (type) : NULL_TREE;
  393.  
  394. #ifdef GATHER_STATISTICS
  395.   n_calls_get_base_type++;
  396. #endif
  397.  
  398.   parent = TYPE_MAIN_VARIANT (parent);
  399.   search_stack = push_search_level (search_stack, &search_obstack);
  400.  
  401.   while (1)
  402.     {
  403.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  404.  
  405.       /* Process and/or queue base types.  */
  406.       for (i = 1; i <= n_baselinks; i++)
  407.     if (CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) == 0)
  408.       {
  409.         int via_private = is_private || !CLASSTYPE_VIA_PUBLIC (type, i);
  410.  
  411.         if (via_private == 0)
  412.           ;
  413.         else if (protect == 0)
  414.           via_private = 0;
  415.         else if (protect == 1 && type == current_class_type)
  416.           /* The immediate base class of the class we are in
  417.          does let its public members through.  */
  418.           via_private = 0;
  419. #ifndef NOJJG
  420.         else if (protect
  421.              && friends != NULL_TREE
  422.              && type == xtype
  423.              && value_member (current_class_type, friends))
  424.           /* Friend types of the most derived type have access
  425.          to its baseclass pointers.  */
  426.           via_private = 0;
  427. #endif
  428.  
  429.         CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) = 1;
  430.         otype = type;
  431.         obstack_ptr_grow (&search_obstack, CLASSTYPE_BASECLASS (type, i));
  432.         obstack_int_grow (&search_obstack, via_private);
  433.         tail += 2;
  434.         if (tail >= search_stack->limit)
  435.           abort ();
  436.       }
  437.     else if (protect && ! CLASSTYPE_VIA_VIRTUAL (type, i))
  438.       {
  439.         error_with_aggr_type (parent, "type `%s' is ambiguous base class for type `%s'",
  440.                   TYPE_NAME_STRING (xtype));
  441.         error ("(base class for types `%s' and `%s')",
  442.            TYPE_NAME_STRING (type),
  443.            TYPE_NAME_STRING (otype));
  444.         rval = error_mark_node;
  445.         break;
  446.       }
  447.  
  448.     dont_queue:
  449.       /* Process head of queue, if one exists.  */
  450.       if (head >= tail)
  451.     break;
  452.  
  453.       type = search_stack->first[head++];
  454.       is_private = (int)search_stack->first[head++];
  455.       if (TYPE_MAIN_VARIANT (type) == parent)
  456.     {
  457.       if (rval == 0)
  458.         {
  459.           rval = type;
  460.           rval_private = is_private;
  461.         }
  462.       goto dont_queue;
  463.     }
  464.     }
  465.   {
  466.     tree *tp = search_stack->first;
  467.     tree *search_tail = tp + tail;
  468.  
  469.     while (tp < search_tail)
  470.       {
  471.     CLASSTYPE_MARKED5 (*tp) = 0;
  472.     tp += 2;
  473.       }
  474.   }
  475.   search_stack = pop_search_level (search_stack);
  476.  
  477.   if (rval == error_mark_node)
  478.     return error_mark_node;
  479.  
  480.   if (rval && protect && rval_private)
  481.     {
  482.       if (protect == 3)
  483.     {
  484.       int i;
  485.  
  486.       for (i = 1; i <= CLASSTYPE_N_BASECLASSES (xtype); i++)
  487.         if (parent == TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (xtype, i)))
  488.           /* It's ok, since it's immedate.  */
  489.           return rval;
  490.     }
  491.       error_with_aggr_type (xtype, "type `%s' is derived from private `%s'",
  492.                 TYPE_NAME_STRING (parent));
  493.       return error_mark_node;
  494.     }
  495.  
  496.   return rval;
  497. }
  498.  
  499. /* Return the number of levels between type PARENT and type TYPE,
  500.    following the leftmost path to PARENT.  If PARENT is its own main
  501.    type variant, then if PARENT appears in different places from TYPE's
  502.    point of view, the leftmost PARENT will be the one chosen.
  503.  
  504.    Return -1 if TYPE is not derived from PARENT.
  505.    Return -2 if PARENT is an ambiguous base class of TYPE.
  506.    Return -3 if PARENT is private to TYPE, and protect is non-zero.
  507.  
  508.    If PATH_PTR is non-NULL, then also build the list of types
  509.    from PARENT to TYPE, with TREE_VIA_VIRUAL and TREE_VIA_PUBLIC
  510.    set.  */
  511. get_base_distance (parent, type, protect, path_ptr)
  512.      register tree parent, type;
  513.      int protect;
  514.      tree *path_ptr;
  515. {
  516.   int head = 0, tail = 0;
  517.   int is_private = 0;
  518.   int rval;
  519.   int depth = 0;
  520.   int rval_private = 0;
  521.   tree basetypes;
  522.   tree friends = current_class_type
  523.     ? CLASSTYPE_FRIEND_CLASSES (type) : NULL_TREE;
  524.   int use_leftmost;
  525.  
  526.   if (TREE_READONLY (parent) || TREE_VOLATILE (parent))
  527.     parent = TYPE_MAIN_VARIANT (parent);
  528.   use_leftmost = (parent == TYPE_MAIN_VARIANT (parent));
  529.  
  530.   if (path_ptr)
  531.     basetypes = CLASSTYPE_AS_LIST (type);
  532.  
  533.   if (TYPE_MAIN_VARIANT (parent) == type)
  534.     {
  535.       /* If the distance is 0, then we don't really need
  536.      a path pointer, but we shouldn't let garbage go back.  */
  537.       if (path_ptr)
  538.     *path_ptr = basetypes;
  539.       return 0;
  540.     }
  541.  
  542.   search_stack = push_search_level (search_stack, &search_obstack);
  543.  
  544.   while (1)
  545.     {
  546.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  547.  
  548.       /* Process and/or queue base types.  */
  549.       for (i = 1; i <= n_baselinks; i++)
  550.     if (CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) == 0)
  551.       {
  552.         tree btypes;
  553.  
  554.         int via_private = is_private || !CLASSTYPE_VIA_PUBLIC (type, i);
  555.  
  556.         if (via_private == 0)
  557.           ;
  558.         else if (protect == 0)
  559.           via_private = 0;
  560. #if 0
  561.         /* 13 Jan, 1990: I guess this is turned off because
  562.            `get_base_type' will emit a more eloquent message
  563.            if a message desired [--Michael].  */
  564.         /* The immediate base class of the class we are in
  565.            does let its public members through.  */
  566.         else if (type == current_class_type)
  567.           via_private = 0;
  568.         else if (protect
  569.              && friends != NULL_TREE
  570.              && type == xtype
  571.              && value_member (current_class_type, friends))
  572.           /* Friend types of the most derived type have access
  573.          to its baseclass pointers.  */
  574.           via_private = 0;
  575. #endif
  576.  
  577.         CLASSTYPE_MARKED5 (CLASSTYPE_BASECLASS (type, i)) = 1;
  578.         obstack_ptr_grow (&search_obstack, CLASSTYPE_BASECLASS (type, i));
  579.  
  580.         obstack_ptr_grow (&search_obstack, depth);
  581.         obstack_int_grow (&search_obstack, via_private);
  582.         if (path_ptr)
  583.           {
  584.         btypes = tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  585.                     basetypes);
  586.         TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  587.         TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  588.         obstack_ptr_grow (&search_obstack, btypes);
  589.         tail += 1;
  590.           }
  591.         tail += 3;
  592.         if (tail >= search_stack->limit)
  593.           abort ();
  594.       }
  595.     else if (! CLASSTYPE_VIA_VIRTUAL (type, i))
  596.       {
  597.         rval = -2;
  598.         goto done;
  599.       }
  600.  
  601.       /* Process head of queue, if one exists.  */
  602.       if (head >= tail)
  603.     {
  604.       rval = -1;
  605.       break;
  606.     }
  607.  
  608.       type = search_stack->first[head++];
  609.       depth = (int)search_stack->first[head++] + 1;
  610.       is_private = (int)search_stack->first[head++];
  611.       if (path_ptr)
  612.     basetypes = search_stack->first[head++];
  613.       if (type == parent
  614.       || (use_leftmost && TYPE_MAIN_VARIANT (type) == parent))
  615.     {
  616.       rval = depth;
  617.       rval_private = is_private;
  618.       break;
  619.     }
  620.     }
  621.  done:
  622.   {
  623.     tree *tp = search_stack->first;
  624.     tree *search_tail = tp + tail;
  625.     int increment = path_ptr ? 4 : 3;
  626.  
  627.     while (tp < search_tail)
  628.       {
  629.     CLASSTYPE_MARKED5 (*tp) = 0;
  630.     tp += increment;
  631.       }
  632.  
  633.     /* Now, guarantee that we are following the leftmost
  634.        path in the chain.  */
  635.     if (use_leftmost
  636.     && rval > 0
  637.     && (DECL_OFFSET (TYPE_NAME (type)) != 0
  638.         || TREE_VIA_VIRTUAL (type)))
  639.       {
  640.     /* Reduce all types yet to be fully processed into
  641.        the base type we are looking for, or NULL_TREE.  */
  642.     for (tp = search_stack->first; tp < search_tail; tp += increment)
  643.       {
  644.         tree *sub_tp, sub_path_ptr;
  645.         int sub_rval;
  646.  
  647.         /* Don't chase down more right-most paths.  */
  648.         if (TREE_VIA_VIRTUAL (*tp)
  649.         || DECL_OFFSET (TYPE_NAME (*tp)) > DECL_OFFSET (TYPE_NAME (type)))
  650.           {
  651.         *tp = NULL_TREE;
  652.         continue;
  653.           }
  654.  
  655.         /* Don't hassle with duplicates.  */
  656.         if (*tp == type)
  657.           goto skip;
  658.  
  659.         for (sub_tp = search_stack->first; sub_tp < tp; sub_tp += increment)
  660.           if (*tp == *sub_tp)
  661.         goto skip;
  662.  
  663.         /* Find this type's TYPE basetype, if it has one.  */
  664.         sub_rval = get_base_distance (parent, *tp, 0, &sub_path_ptr);
  665.         if (sub_rval == -1)
  666.           *tp = NULL_TREE;
  667.         else
  668.           {
  669.         if (path_ptr && TREE_CHAIN (tp[3]))
  670.           {
  671.             tree last;
  672.             tree next_to_last = sub_path_ptr;
  673.             while (TREE_CHAIN (next_to_last)
  674.                && TREE_CHAIN (TREE_CHAIN (next_to_last)))
  675.               next_to_last = TREE_CHAIN (next_to_last);
  676.             if (next_to_last == sub_path_ptr)
  677.               {
  678.             sub_path_ptr = copy_node (sub_path_ptr);
  679.             last = sub_path_ptr;
  680.               }
  681.             else
  682.               {
  683.             last = copy_node (TREE_CHAIN (next_to_last));
  684.             TREE_CHAIN (next_to_last) = last;
  685.               }
  686.             TREE_CHAIN (last) = TREE_CHAIN (tp[3]);
  687.           }
  688.         *tp = TREE_VALUE (sub_path_ptr);
  689.         if (path_ptr)
  690.           tp[3] = sub_path_ptr;
  691.           }
  692.       skip: {}
  693.       }
  694.  
  695.     /* For all the types which reduce to TYPE, choose
  696.        the leftmost non-virtual one of them.  */
  697.     for (tp = search_stack->first; tp < search_tail; tp += increment)
  698.       {
  699.         if (*tp == NULL_TREE)
  700.           continue;
  701.  
  702.         if (DECL_OFFSET (TYPE_NAME (*tp)) < DECL_OFFSET (TYPE_NAME (type)))
  703.           {
  704.         rval = -2;
  705.         type = *tp;
  706.         if (path_ptr)
  707.           basetypes = tp[3];
  708.           }
  709.       }
  710.     if (rval == -2)
  711.       rval_private = 0;
  712.       }
  713.   }
  714.   search_stack = pop_search_level (search_stack);
  715.  
  716.   if (rval && protect && rval_private)
  717.     return -3;
  718.  
  719.   if (path_ptr)
  720.     *path_ptr = basetypes;
  721.   return rval;
  722. }
  723.  
  724. /* Search for a member with name NAME in a multiple inheritance lattice
  725.    specified by TYPE.  If it does not exist, return NULL_TREE.
  726.    If the member is ambiguously referenced, return `error_mark_node'.
  727.    Otherwise, return the FIELD_DECL.  */
  728.  
  729. /* Do a 1-level search for NAME as a member of TYPE.  The caller
  730.    must figure out whether it has a visible path to this field.
  731.    (Since it is only one level, this is reasonable.)  */
  732. static tree
  733. lookup_field_1 (type, name)
  734.      tree type, name;
  735. {
  736.   register tree field = TYPE_FIELDS (type);
  737.  
  738. #ifdef GATHER_STATISTICS
  739.   n_calls_lookup_field_1++;
  740. #endif
  741.   while (field)
  742.     {
  743. #ifdef GATHER_STATISTICS
  744.       n_fields_searched++;
  745. #endif
  746.       if (DECL_ANON_UNION_ELEM (field))
  747.     {
  748.       tree temp = lookup_field_1 (TREE_TYPE (field), name);
  749.       if (temp)
  750.         return temp;
  751.     }
  752.       if (DECL_NAME (field) == name)
  753.     return field;
  754.       field = TREE_CHAIN (field);
  755.     }
  756.   /* Not found.  */
  757.   if (name == _vptr_name)
  758.     {
  759.       /* Give the user what s/he thinks s/he wants.  */
  760.       if (TYPE_VIRTUAL_P (type))
  761.     return CLASSTYPE_VFIELD (type);
  762.     }
  763.   return NULL_TREE;
  764. }
  765.  
  766. /* Compute the visibility of FIELD.  This is done by computing
  767.    the visibility available to each type in BASETYPES (which comes
  768.    as a list of [via_public/basetype] in reverse order, namely base
  769.    class before derived class).  The first one which defines a
  770.    visibility defines the visibility for the field.  Otherwise, the
  771.    visibility of the field is that which occurs normally.
  772.  
  773.    Uses global variables CURRENT_CLASS_TYPE and
  774.    CURRENT_FUNCTION_DECL to use friend relationships
  775.    if necessary.
  776.  
  777.    This will be static when lookup_fnfield comes into this file.  */
  778.  
  779. #define PUBLIC_RETURN do { TREE_FIELD_PUBLIC (field) = 1; return visibility_public; } while (0)
  780. #define PROTECTED_RETURN do { TREE_FIELD_PROTECTED (field) = 1; return visibility_protected; } while (0)
  781. #define PRIVATE_RETURN do { TREE_FIELD_PRIVATE (field) = 1; return visibility_private; } while (0)
  782.  
  783. enum visibility_type
  784. compute_visibility (basetypes, field)
  785.      tree basetypes, field;
  786. {
  787.   enum visibility_type visibility = visibility_public;
  788.   tree types, type;
  789.   tree context = DECL_FIELD_CONTEXT (field);
  790.  
  791.   /* Virtual function tables are never private.
  792.      But we should know that we are looking for this,
  793.      and not even try to hide it.  */
  794.   if (VFIELD_NAME_P (DECL_NAME (field)) == 1)
  795.     return visibility_public;
  796.  
  797.   /* Make these special cases fast.  */
  798.   if (TREE_VALUE (basetypes) == current_class_type)
  799.     {
  800.       if (TREE_FIELD_PUBLIC (field))
  801.     return visibility_public;
  802.       if (TREE_FIELD_PROTECTED (field))
  803.     return visibility_protected;
  804.       if (TREE_FIELD_PRIVATE (field))
  805.     return visibility_private;
  806.     }
  807.  
  808.   /* Member function manipulating its own members.  */
  809.   if (current_class_type == context)
  810.     PUBLIC_RETURN;
  811.  
  812.   /* Member found immediately within object.  */
  813.   if (TREE_CHAIN (basetypes) == NULL_TREE)
  814.     {
  815.       /* At object's top level, public members are public.  */
  816.       if (TREE_PROTECTED (field) == 0 && TREE_PRIVATE (field) == 0)
  817.     PUBLIC_RETURN;
  818.  
  819.       /* Friend function manipulating members it gets (for being a friend).  */
  820.       if (is_friend (context, current_function_decl))
  821.     PUBLIC_RETURN;
  822.  
  823.       /* Inner than that, without special visibility,
  824.  
  825.        protected members are ok if type of object is current_class_type
  826.        is derived therefrom.  This means that if the type of the object
  827.        is a base type for our current class type, we cannot access
  828.        protected members.
  829.  
  830.        private members are not ok.  */
  831.       if (current_class_type && DECL_VISIBILITY (field) == NULL_TREE)
  832.     {
  833.       if (TREE_PRIVATE (field))
  834.         PRIVATE_RETURN;
  835.  
  836.       if (TREE_PROTECTED (field))
  837.         {
  838.           if (context == current_class_type
  839.           || (type = get_base_type (current_class_type, context, 0)))
  840.         PUBLIC_RETURN;
  841.           else
  842.         PROTECTED_RETURN;
  843.         }
  844.       else abort ();
  845.     }
  846.     }
  847.   /* Friend function manipulating members it gets (for being a friend).  */
  848.   if (is_friend (context, current_function_decl))
  849.     PUBLIC_RETURN;
  850.  
  851.   /* must reverse more than one element */
  852.   basetypes = nreverse (basetypes);
  853.  
  854.   types = basetypes;
  855.  
  856.   while (types)
  857.     {
  858.       tree member;
  859.       type = TYPE_MAIN_VARIANT (TREE_VALUE (types));
  860.  
  861.       member = purpose_member (type, DECL_VISIBILITY (field));
  862.       if (member)
  863.     {
  864.       visibility = (enum visibility_type)TREE_VALUE (member);
  865.       if (visibility == visibility_public
  866.           || is_friend (type, current_function_decl)
  867.           || (visibility == visibility_protected
  868.           && current_class_type
  869.           && get_base_type (context, current_class_type, 0)))
  870.         visibility = visibility_public;
  871.       goto ret;
  872.     }
  873.  
  874.       /* Friends inherit the visibility of the class they inherit from.  */
  875.       if (is_friend (type, current_function_decl))
  876.     {
  877.       if (type == context)
  878.         {
  879.           visibility = visibility_public;
  880.           goto ret;
  881.         }
  882.       if (TREE_PROTECTED (field))
  883.         {
  884.           visibility = visibility_public;
  885.           goto ret;
  886.         }
  887. #if 0
  888.       /* This short-cut is too short.  */
  889.       if (visibility == visibility_public)
  890.         goto ret;
  891. #endif
  892.       /* else, may be a friend of a deeper base class */
  893.     }
  894.  
  895.       if (type == context)
  896.     break;
  897.  
  898.       types = TREE_CHAIN (types);
  899.       /* If the next type was not VIA_PUBLIC, then fields of all
  900.      remaining class past that one are private.  */
  901.       if (types && ! TREE_VIA_PUBLIC (types))
  902.     visibility = visibility_private;
  903.     }
  904.  
  905.   /* No special visibilities apply.  Use normal rules.
  906.      No assignment needed for BASETYPEs here from the nreverse.
  907.      This is because we use it only for information about the
  908.      path to the base.  The code earlier dealt with what
  909.      happens when we are at the base level.  */
  910.  
  911.   if (visibility == visibility_public)
  912.     {
  913.       basetypes = nreverse (basetypes);
  914.       if (TREE_PRIVATE (field))
  915.     PRIVATE_RETURN;
  916.       if (TREE_PROTECTED (field))
  917.     {
  918.       /* Used to check if the current class type was derived from
  919.          the type that contains the field.  This is wrong for
  920.          multiple inheritance because is gives one class reference
  921.          to protected members via another classes protected path.
  922.          I.e., if A; B1 : A; B2 : A;  Then B1 and B2 can access
  923.          their own members which are protected in A, but not
  924.          those same members in one another.  */
  925.       if (
  926. #if 1
  927.           current_class_type
  928.           && get_base_type (context, current_class_type, 0)
  929. #else
  930.           current_class_type
  931.           && value_member (current_class_type, basetypes)
  932. #endif
  933.           )
  934.         PUBLIC_RETURN;
  935.       PROTECTED_RETURN;
  936.     }
  937.       PUBLIC_RETURN;
  938.     }
  939.  
  940.   if (visibility == visibility_private
  941.       && current_class_type != NULL_TREE)
  942.     {
  943.       if (TREE_PRIVATE (field))
  944.     {
  945.       nreverse (basetypes);
  946.       PRIVATE_RETURN;
  947.     }
  948.  
  949.       /* See if the field isn't protected.  */
  950.       if (TREE_PROTECTED (field))
  951.     {
  952.       tree test;
  953. #if 0
  954.       test = get_base_type (type, current_class_type, 0);
  955. #else
  956.       test = value_member (current_class_type, basetypes);
  957. #endif
  958.       nreverse (basetypes);
  959.       if (test)
  960.         PUBLIC_RETURN;
  961.       PROTECTED_RETURN;
  962.     }
  963.  
  964.       /* See if the field isn't a public member of
  965.      a private base class.  */
  966.  
  967.       visibility = visibility_public;
  968.       types = TREE_CHAIN (basetypes);
  969.       while (types)
  970.     {
  971.       if (! TREE_VIA_PUBLIC (types))
  972.         {
  973.           if (visibility == visibility_private)
  974.         {
  975.           visibility = visibility_private;
  976.           goto ret;
  977.         }
  978.           visibility = visibility_private;
  979.         }
  980.       if (TYPE_MAIN_VARIANT (TREE_VALUE (types)) == context)
  981.         {
  982.           visibility = visibility_public;
  983.           goto ret;
  984.         }
  985.       types = TREE_CHAIN (types);
  986.     }
  987.       abort ();
  988.     }
  989.  
  990.  ret:
  991.   nreverse (basetypes);
  992.  
  993.   if (visibility == visibility_public)
  994.     TREE_FIELD_PUBLIC (field) = 1;
  995.   else if (visibility == visibility_protected)
  996.     TREE_FIELD_PROTECTED (field) = 1;
  997.   else if (visibility == visibility_private)
  998.     TREE_FIELD_PRIVATE (field) = 1;
  999.   else abort ();
  1000.   return visibility;
  1001. }
  1002.  
  1003. /* Make an entry in the memoized table for type TYPE
  1004.    that the entry for NAME is FIELD.  */
  1005.  
  1006. tree
  1007. make_memoized_table_entry (type, name, function_p)
  1008.      tree type, name;
  1009.      int function_p;     
  1010. {
  1011.   int index = MEMOIZED_HASH_FN (name);
  1012.   tree entry, *prev_entry;
  1013.  
  1014.   memoized_adds[function_p] += 1;
  1015.   if (CLASSTYPE_MTABLE_ENTRY (type) == NULL_TREE)
  1016.     {
  1017.       obstack_ptr_grow (&type_obstack, type);
  1018.       obstack_blank (&type_obstack, sizeof (struct memoized_entry *));
  1019.       CLASSTYPE_MTABLE_ENTRY (type) = my_new_memoized_entry (0);
  1020.       type_stack->len++;
  1021.       if (type_stack->len * 2 >= type_stack->base.limit)
  1022.     abort ();
  1023.     }
  1024.   if (function_p)
  1025.     prev_entry = &MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1026.   else
  1027.     prev_entry = &MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (type), index);
  1028.  
  1029.   entry = my_tree_cons (name, 0, *prev_entry);
  1030.   *prev_entry = entry;
  1031.  
  1032.   /* Don't know the error message to give yet.  */
  1033.   TREE_TYPE (entry) = error_mark_node;
  1034.  
  1035.   return entry;
  1036. }
  1037.  
  1038. tree
  1039. lookup_field (xbasetype, name, protect)
  1040.      register tree xbasetype, name;
  1041.      int protect;
  1042. {
  1043.   int head = 0, tail = 0;
  1044.   tree type, rval;
  1045.   tree basetype, basetypes;
  1046.   enum visibility_type this_v = visibility_default;
  1047.   tree entry;
  1048.   enum visibility_type own_visibility = visibility_default;
  1049.   int vbase_name_p = VBASE_NAME_P (name);
  1050.  
  1051.   /* Things for memoization.  */
  1052.   char *errstr = 0;
  1053.  
  1054.   /* Set this to nonzero if we don't know how to compute
  1055.      accurate error messages for visibility.  */
  1056.   int index = MEMOIZED_HASH_FN (name);
  1057.  
  1058.   if (TREE_CODE (xbasetype) == TREE_LIST)
  1059.     basetypes = xbasetype, basetype = TREE_VALUE (xbasetype);
  1060.   else
  1061.     basetypes = CLASSTYPE_AS_LIST (xbasetype), basetype = xbasetype;
  1062.  
  1063.   if (CLASSTYPE_MTABLE_ENTRY (basetype))
  1064.     {
  1065.       tree tem = MEMOIZED_FIELDS (CLASSTYPE_MTABLE_ENTRY (basetype), index);
  1066.  
  1067.       while (tem && TREE_PURPOSE (tem) != name)
  1068.     {
  1069.       memoized_fields_searched[0]++;
  1070.       tem = TREE_CHAIN (tem);
  1071.     }
  1072.       if (tem)
  1073.     {
  1074.       if (protect && TREE_TYPE (tem))
  1075.         {
  1076.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1077.              IDENTIFIER_POINTER (name),
  1078.              TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (tem))));
  1079.           return error_mark_node;
  1080.         }
  1081.       if (TREE_VALUE (tem) == NULL_TREE)
  1082.         memoized_fast_rejects[0] += 1;
  1083.       else
  1084.         memoized_fast_finds[0] += 1;
  1085.       return TREE_VALUE (tem);
  1086.     }
  1087.     }
  1088.  
  1089. #ifdef GATHER_STATISTICS
  1090.   n_calls_lookup_field++;
  1091. #endif
  1092.   if (flag_memoize_lookups && ! global_bindings_p ())
  1093.     entry = make_memoized_table_entry (basetype, name, 0);
  1094.   else
  1095.     entry = 0;
  1096.  
  1097.   rval = lookup_field_1 (basetype, name);
  1098.  
  1099.   if (rval)
  1100.     {
  1101.       if (flag_memoize_lookups || protect)
  1102.     {
  1103.       if (TREE_PRIVATE (rval) | TREE_PROTECTED (rval))
  1104.         this_v = compute_visibility (basetypes, rval);
  1105.       if (TREE_CODE (rval) == CONST_DECL)
  1106.         {
  1107.           if (this_v == visibility_private)
  1108.         errstr = "enum `%s' is a private value of class `%s'";
  1109.           else if (this_v == visibility_protected)
  1110.         errstr = "enum `%s' is a protected value of class `%s'";
  1111.         }
  1112.       else
  1113.         {
  1114.           if (this_v == visibility_private)
  1115.         errstr = "member `%s' is a private member of class `%s'";
  1116.           else if (this_v == visibility_protected)
  1117.         errstr = "member `%s' is a protected member of class `%s'";
  1118.         }
  1119.     }
  1120.  
  1121.       if (entry)
  1122.     {
  1123.       if (errstr)
  1124.         {
  1125.           /* This depends on behavior of lookup_field_1!  */
  1126.           tree error_string = my_build_string (errstr);
  1127.           TREE_TYPE (entry) = error_string;
  1128.         }
  1129.       else
  1130.         {
  1131.           /* Let entry know there is no problem with this access.  */
  1132.           TREE_TYPE (entry) = NULL_TREE;
  1133. #if 0
  1134.           /* And since everything is ok, bear the
  1135.          cost of generating correct code.  */
  1136.           if (DECL_OFFSET (TYPE_NAME (basetype)) != 0
  1137.           || TREE_VIA_VIRTUAL (basetype))
  1138.         {
  1139.           rval = my_copy_node (rval);
  1140.           DECL_FIELD_CONTEXT (rval) = basetype;
  1141.         }
  1142. #endif
  1143.         }
  1144.       TREE_VALUE (entry) = rval;
  1145.     }
  1146. #if 0
  1147.       else if ((DECL_OFFSET (TYPE_NAME (basetype)) != 0
  1148.         || TREE_VIA_VIRTUAL (basetype))
  1149.            && ! (errstr && protect))
  1150.     {
  1151.       rval = my_copy_node (rval);
  1152.       DECL_FIELD_CONTEXT (rval) = basetype;
  1153.     }
  1154. #endif
  1155.  
  1156.       if (errstr && protect)
  1157.     {
  1158.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (basetype));
  1159.       return error_mark_node;
  1160.     }
  1161.       return rval;
  1162.     }
  1163.  
  1164.   type = TYPE_MAIN_VARIANT (basetype);
  1165.  
  1166.   search_stack = push_search_level (search_stack, &search_obstack);
  1167.   TREE_VIA_PUBLIC (basetypes) = 1;
  1168.  
  1169.   while (1)
  1170.     {
  1171.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1172.  
  1173.       /* Process and/or queue base types.  */
  1174.       for (i = 1; i <= n_baselinks; i++)
  1175.     {
  1176.       if (CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0)
  1177.         {
  1178.           tree btypes;
  1179.  
  1180.           CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) = 1;
  1181.           btypes = my_tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1182.                      basetypes);
  1183.           TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  1184.           TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  1185.           obstack_ptr_grow (&search_obstack, btypes);
  1186.           tail += 1;
  1187.           if (tail >= search_stack->limit)
  1188.         abort ();
  1189.         }
  1190.     }
  1191.  
  1192.       /* Process head of queue, if one exists.  */
  1193.       if (head >= tail)
  1194.     break;
  1195.  
  1196.       basetypes = search_stack->first[head++];
  1197.       type = TREE_VALUE (basetypes);
  1198.  
  1199.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1200.      and we do find NAME in TYPE, verify that such a second
  1201.      sighting is in fact legal.  */
  1202.  
  1203.       if (rval)
  1204.     {
  1205.       /* Just another way of finding the same member.  */
  1206.       if (DECL_FIELD_CONTEXT (rval) == type)
  1207.         {
  1208.           enum visibility_type new_v
  1209.         = compute_visibility (basetypes, rval);
  1210.           if (this_v != new_v)
  1211.         errstr = "conflicting visibilities to member `%s'";
  1212.         }
  1213.       /* Same baseclass, different places in the lattice.  */
  1214.       else if (DECL_FIELD_CONTEXT (rval) == TYPE_MAIN_VARIANT (type))
  1215.         errstr = "member `%s' belongs to distinct base classes `%s'";
  1216.       else
  1217.         {
  1218.           tree nval = lookup_field_1 (type, name);
  1219.  
  1220.           if (nval && get_base_type (type, DECL_FIELD_CONTEXT (rval), 0) == 0)
  1221.         {
  1222.           /* We found it in other than a baseclass of RVAL's.  */
  1223.           errstr = "request for member `%s' is ambiguous";
  1224.         }
  1225.         }
  1226.       if (errstr && entry)
  1227.         {
  1228.           tree error_string = my_build_string (errstr);
  1229.           TREE_TYPE (entry) = error_string;
  1230.         }
  1231.       if (errstr && protect)
  1232.         break;
  1233.     }
  1234.       else
  1235.     {
  1236.       rval = lookup_field_1 (type, name);
  1237.       if (rval)
  1238.         {
  1239. #if 0
  1240.           if (DECL_OFFSET (TYPE_NAME (type)) != 0
  1241.           || TREE_VIA_VIRTUAL (type))
  1242.         {
  1243.           rval = my_copy_node (rval);
  1244.           DECL_FIELD_CONTEXT (rval) = type;
  1245.         }
  1246. #endif
  1247.  
  1248.           if (entry || protect)
  1249.         this_v = compute_visibility (basetypes, rval);
  1250.           if (entry)
  1251.         TREE_VALUE (entry) = rval;
  1252.  
  1253.           /* These may look ambiguous, but they really are not.  */
  1254.           if (vbase_name_p)
  1255.         break;
  1256.         }
  1257.     }
  1258.     }
  1259.   {
  1260.     tree *tp = search_stack->first;
  1261.     tree *search_tail = tp + tail;
  1262.  
  1263.     /* If this FIELD_DECL defines its own visibility, deal with that.  */
  1264.     if (rval && errstr == 0
  1265.     && DECL_VISIBILITY (rval)
  1266.     && (protect || entry))
  1267.       {
  1268.     while (tp < search_tail)
  1269.       {
  1270.         /* If is possible for one of the derived types on the
  1271.            path to have defined special visibility for this
  1272.            field.  Look for such declarations and report an
  1273.            error if a conflict is found.  */
  1274.         enum visibility_type new_v;
  1275.  
  1276.         if (this_v != visibility_default)
  1277.           new_v = compute_visibility (*tp, rval);
  1278.         if (this_v != visibility_default && new_v != this_v)
  1279.           {
  1280.         errstr = "conflicting visibilities to member `%s'";
  1281.         this_v = visibility_default;
  1282.           }
  1283.         own_visibility = new_v;
  1284.         CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1285.       }
  1286.       }
  1287.     else
  1288.       {
  1289.     while (tp < search_tail)
  1290.       CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1291.       }
  1292.   }
  1293.   search_stack = pop_search_level (search_stack);
  1294.  
  1295.   if (errstr == 0)
  1296.     {
  1297.       if (own_visibility == visibility_private)
  1298.     errstr = "member `%s' declared private";
  1299.       else if (own_visibility == visibility_protected)
  1300.     errstr = "member `%s' declared protected";
  1301.       else if (this_v == visibility_private)
  1302.     errstr = TREE_PRIVATE (rval) ? "member `%s' is private" : "member `%s' is from private base class";
  1303.       else if (this_v == visibility_protected)
  1304.     errstr = "member `%s' is protected";
  1305.     }
  1306.  
  1307.   if (entry)
  1308.     {
  1309.       if (errstr)
  1310.     {
  1311.       tree error_string = my_build_string (errstr);
  1312.       /* Save error message with entry.  */
  1313.       TREE_TYPE (entry) = error_string;
  1314.     }
  1315.       else
  1316.     {
  1317.       /* Mark entry as having no error string.  */
  1318.       TREE_TYPE (entry) = NULL_TREE;
  1319.     }
  1320.     }
  1321.  
  1322.   if (errstr && protect)
  1323.     {
  1324.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (type));
  1325.       rval = error_mark_node;
  1326.     }
  1327.   return rval;
  1328. }
  1329.  
  1330. /* TYPE is a class type. Return the index of the fields within
  1331.    the method vector with name NAME, or -1 is no such field exists.  */
  1332. static int
  1333. lookup_fnfields_1 (type, name)
  1334.      tree type, name;
  1335. {
  1336.   register tree method_vec = CLASSTYPE_METHOD_VEC (type);
  1337.  
  1338.   if (method_vec != 0)
  1339.     {
  1340.       register tree *methods = &TREE_VEC_ELT (method_vec, 0);
  1341.       register tree *end = TREE_VEC_END (method_vec);
  1342.  
  1343. #ifdef GATHER_STATISTICS
  1344.       n_calls_lookup_fnfields_1++;
  1345. #endif
  1346.       if (*methods == 0)
  1347.     methods++;
  1348.       while (methods != end)
  1349.     {
  1350. #ifdef GATHER_STATISTICS
  1351.       n_outer_fields_searched++;    
  1352. #endif
  1353.       if (DECL_ORIGINAL_NAME (*methods) == name)
  1354.         break;
  1355.       methods++;
  1356.     }
  1357.       if (methods != end)
  1358.     return methods - &TREE_VEC_ELT (method_vec, 0);
  1359.     }
  1360.  
  1361.   return -1;
  1362. }
  1363.  
  1364. /* Given a list of member functions FIELDS (which are implicitly
  1365.    named TREE_PURPOSE (FIELDS), and come from base type
  1366.    DECL_FIELD_CONTEXT (TREE_VALUE (FIELDS))), attempt to find the
  1367.    actual method which can accept (using conversions) PARMS.
  1368.    The types of PARMS are already computed in PARMTYPES.  */
  1369. tree
  1370. lookup_fnfield (fields, parms, parmtypes)
  1371.      tree fields, parms, parmtypes;
  1372. {
  1373.   abort ();
  1374. }
  1375.  
  1376. /* Starting from BASETYPE, return a TREE_BASELINK-like object
  1377.    which gives the following information (in a list):
  1378.  
  1379.    TREE_TYPE: list of basetypes needed to get to...
  1380.    TREE_VALUE: list of all functions in of given type
  1381.    which have name NAME.
  1382.  
  1383.    No visibility information is computed by this function,
  1384.    other then to adorn the list of basetypes with
  1385.    TREE_VIA_PUBLIC.
  1386.  
  1387.    If FIND_AMBIGUOUS is non-zero, then if we find two ways to get
  1388.    to the same member function, both those ways are found,
  1389.    and the caller must know what to do about this.  */
  1390. tree
  1391. lookup_fnfields (basetypes, name, find_ambiguous)
  1392.      tree basetypes, name;
  1393.      int find_ambiguous;
  1394. {
  1395.   int head = 0, tail = 0;
  1396.   tree type, rval, rvals = NULL_TREE;
  1397.   tree basetype;
  1398.   tree entry;
  1399.  
  1400.   /* For now, don't try this.  */
  1401.   int protect = find_ambiguous;
  1402.  
  1403.   /* Things for memoization.  */
  1404.   char *errstr = 0;
  1405.  
  1406.   /* Set this to nonzero if we don't know how to compute
  1407.      accurate error messages for visibility.  */
  1408.   int index = MEMOIZED_HASH_FN (name);
  1409.  
  1410.   basetype = TREE_VALUE (basetypes);
  1411.  
  1412.   if (CLASSTYPE_MTABLE_ENTRY (basetype))
  1413.     {
  1414.       tree tem = MEMOIZED_FNFIELDS (CLASSTYPE_MTABLE_ENTRY (basetype), index);
  1415.  
  1416.       while (tem && TREE_PURPOSE (tem) != name)
  1417.     {
  1418.       memoized_fields_searched[1]++;
  1419.       tem = TREE_CHAIN (tem);
  1420.     }
  1421.       if (tem)
  1422.     {
  1423.       if (protect && TREE_TYPE (tem))
  1424.         {
  1425.           error (TREE_STRING_POINTER (TREE_TYPE (tem)),
  1426.              IDENTIFIER_POINTER (name),
  1427.              TYPE_NAME_STRING (DECL_FIELD_CONTEXT (TREE_VALUE (TREE_VALUE (tem)))));
  1428.           return error_mark_node;
  1429.         }
  1430.       if (TREE_VALUE (tem) == NULL_TREE)
  1431.         {
  1432.           memoized_fast_rejects[1] += 1;
  1433.           return NULL_TREE;
  1434.         }
  1435.       else
  1436.         {
  1437.           /* Want to return this, but we must make sure
  1438.          that visibility information is consistent.  */
  1439.           tree baselink = TREE_VALUE (tem);
  1440.           tree memoized_basetypes = TREE_PURPOSE (baselink);
  1441.           tree these_basetypes = basetypes;
  1442.           while (memoized_basetypes && these_basetypes)
  1443.         {
  1444.           memoized_fields_searched[1]++;
  1445.           if (TREE_VALUE (memoized_basetypes) != TREE_VALUE (these_basetypes))
  1446.             break;
  1447.           memoized_basetypes = TREE_CHAIN (memoized_basetypes);
  1448.           these_basetypes = TREE_CHAIN (these_basetypes);
  1449.         }
  1450.           if (memoized_basetypes == these_basetypes)
  1451.         {
  1452.           memoized_fast_finds[1] += 1;
  1453.           return TREE_VALUE (tem);
  1454.         }
  1455.           /* else, we must re-find this field by hand.  */
  1456.           baselink = tree_cons (basetypes, TREE_VALUE (baselink), TREE_CHAIN (baselink));
  1457.           return baselink;
  1458.         }
  1459.     }
  1460.     }
  1461.  
  1462. #ifdef GATHER_STATISTICS
  1463.   n_calls_lookup_fnfields++;
  1464. #endif
  1465.   if (flag_memoize_lookups && ! global_bindings_p ())
  1466.     entry = make_memoized_table_entry (basetype, name, 1);
  1467.   else
  1468.     entry = 0;
  1469.  
  1470.   index = lookup_fnfields_1 (basetype, name);
  1471.  
  1472.   if (index >= 0)
  1473.     {
  1474.       rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), index);
  1475.       rvals = my_tree_cons (basetypes, rval, NULL_TREE);
  1476.       if (CLASSTYPE_BASELINK_VEC (basetype))
  1477.     TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (basetype), index);
  1478.  
  1479.       if (entry)
  1480.     {
  1481.       TREE_VALUE (entry) = rvals;
  1482.       TREE_TYPE (entry) = NULL_TREE;
  1483.     }
  1484.  
  1485.       if (errstr && protect)
  1486.     {
  1487.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (basetype));
  1488.       return error_mark_node;
  1489.     }
  1490.       return rvals;
  1491.     }
  1492.   rval = NULL_TREE;
  1493.   type = TYPE_MAIN_VARIANT (basetype);
  1494.  
  1495.   search_stack = push_search_level (search_stack, &search_obstack);
  1496.   TREE_VIA_PUBLIC (basetypes) = 1;
  1497.  
  1498.   while (1)
  1499.     {
  1500.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1501.  
  1502.       /* Process and/or queue base types.  */
  1503.       for (i = 1; i <= n_baselinks; i++)
  1504.     {
  1505.       if (CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0)
  1506.         {
  1507.           tree btypes;
  1508.  
  1509.           CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) = 1;
  1510.           btypes = my_tree_cons (NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1511.                      basetypes);
  1512.           TREE_VIA_PUBLIC (btypes) = CLASSTYPE_VIA_PUBLIC (type, i);
  1513.           TREE_VIA_VIRTUAL (btypes) = CLASSTYPE_VIA_VIRTUAL (type, i);
  1514.           obstack_ptr_grow (&search_obstack, btypes);
  1515.           tail += 1;
  1516.           if (tail >= search_stack->limit)
  1517.         abort ();
  1518.         }
  1519.     }
  1520.  
  1521.       /* Process head of queue, if one exists.  */
  1522.       if (head >= tail)
  1523.     break;
  1524.  
  1525.       basetypes = search_stack->first[head++];
  1526.       type = TREE_VALUE (basetypes);
  1527.  
  1528.       /* See if we can find NAME in TYPE.  If RVAL is nonzero,
  1529.      and we do find NAME in TYPE, verify that such a second
  1530.      sighting is in fact legal.  */
  1531.  
  1532.       if (rval)
  1533.     {
  1534.       tree context = DECL_FIELD_CONTEXT (rval);
  1535.       /* Just another way of finding the same member.  */
  1536.       if (context == type)
  1537.         ;
  1538.       /* Same baseclass, maybe different places in the lattice.  */
  1539.       else if (context == TYPE_MAIN_VARIANT (type))
  1540.         {
  1541.           if (TREE_VIA_VIRTUAL (TREE_PURPOSE (rvals)))
  1542.         if (TREE_VIA_VIRTUAL (basetypes))
  1543.           ;
  1544.         else
  1545.           errstr = "member `%s' belongs to virtual and non-virtual baseclasses `%s'";
  1546.           else if (TREE_VIA_VIRTUAL (basetypes))
  1547.         errstr = "member `%s' belongs to virtual and non-virtual baseclasses `%s'";
  1548.           else
  1549.         errstr = "member `%s' belongs to MI-distinct base classes `%s'";
  1550.         }
  1551.       else
  1552.         {
  1553.           int index = lookup_fnfields_1 (type, name);
  1554.  
  1555.           if (index >= 0 && get_base_type (type, context, 0) == 0)
  1556.         {
  1557.           /* We found it in other than a baseclass of RVAL's.  */
  1558.           rvals = my_tree_cons (basetypes, TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index), rvals);
  1559.           if (CLASSTYPE_BASELINK_VEC (type))
  1560.             TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1561.         }
  1562.         }
  1563.       if (errstr && entry)
  1564.         {
  1565.           tree error_string = my_build_string (errstr);
  1566.           TREE_TYPE (entry) = error_string;
  1567.         }
  1568.       if (errstr && find_ambiguous)
  1569.         {
  1570.           rvals = error_mark_node;
  1571.           break;
  1572.         }
  1573.     }
  1574.       else
  1575.     {
  1576.       int index = lookup_fnfields_1 (type, name);
  1577.       if (index >= 0)
  1578.         {
  1579.           rval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1580.           rvals = my_tree_cons (basetypes, rval, NULL_TREE);
  1581.           if (CLASSTYPE_BASELINK_VEC (type))
  1582.         TREE_TYPE (rvals) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1583.           if (entry)
  1584.         TREE_VALUE (entry) = rvals;
  1585.         }
  1586.       else
  1587.         rval = NULL_TREE;
  1588.     }
  1589.     }
  1590.   {
  1591.     tree *tp = search_stack->first;
  1592.     tree *search_tail = tp + tail;
  1593.  
  1594.     while (tp < search_tail)
  1595.       CLASSTYPE_MARKED2 (TREE_VALUE (*tp++)) = 0;
  1596.   }
  1597.   search_stack = pop_search_level (search_stack);
  1598.  
  1599.   if (entry)
  1600.     {
  1601.       if (errstr)
  1602.     {
  1603.       tree error_string = my_build_string (errstr);
  1604.       /* Save error message with entry.  */
  1605.       TREE_TYPE (entry) = error_string;
  1606.     }
  1607.       else
  1608.     {
  1609.       /* Mark entry as having no error string.  */
  1610.       TREE_TYPE (entry) = NULL_TREE;
  1611.     }
  1612.     }
  1613.  
  1614.   if (errstr && protect)
  1615.     {
  1616.       error (errstr, IDENTIFIER_POINTER (name), TYPE_NAME_STRING (type));
  1617.       rvals = error_mark_node;
  1618.     }
  1619.  
  1620.   return rvals;
  1621. }
  1622.  
  1623. /* BREADTH-FIRST SEARCH ROUTINES.  */
  1624.  
  1625. /* Search a multiple inheritance hierarchy by breadth-first search.
  1626.  
  1627.    TYPE is an aggregate type, possibly in a multiple-inheritance hierarchy.
  1628.    TESTFN is a function, which, if true, means that our condition has been met,
  1629.    and its return value should be returned.
  1630.    QFN, if non-NULL, is a predicate dictating whether the type should
  1631.    even be queued.  */
  1632.  
  1633. int
  1634. breadth_first_search (type, testfn, qfn)
  1635.      tree type;
  1636.      int (*testfn)();
  1637.      int (*qfn)();
  1638. {
  1639.   int head = 0, tail = 0;
  1640.   int rval = 0;
  1641.  
  1642.   search_stack = push_search_level (search_stack, &search_obstack);
  1643.  
  1644.   while (1)
  1645.     {
  1646.       int n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1647.       int i;
  1648.  
  1649.       /* Process and/or queue base types.  */
  1650.       for (i = 1; i <= n_baselinks; i++)
  1651.     if (CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0
  1652.         && (qfn == 0 || (*qfn) (type, i)))
  1653.       {
  1654.         CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 1;
  1655.         obstack_ptr_grow (&search_obstack, type);
  1656.         obstack_int_grow (&search_obstack, i);
  1657.         tail += 2;
  1658.         if (tail >= search_stack->limit)
  1659.           abort ();
  1660.       }
  1661.  
  1662.       /* Process head of queue, if one exists.  */
  1663.       if (head >= tail)
  1664.     {
  1665.       rval = 0;
  1666.       break;
  1667.     }
  1668.  
  1669.       type = search_stack->first[head++];
  1670.       i = (int)search_stack->first[head++];
  1671.       if (rval = (*testfn) (type, i))
  1672.     break;
  1673.       type = CLASSTYPE_BASECLASS (type, i);
  1674.     }
  1675.   {
  1676.     tree *tp = search_stack->first;
  1677.     tree *search_tail = tp + tail;
  1678.     while (tp < search_tail)
  1679.       {
  1680.     tree type = *tp++;
  1681.     int i = (int)(*tp++);
  1682.     CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 0;
  1683.       }
  1684.   }
  1685.  
  1686.   search_stack = pop_search_level (search_stack);
  1687.   return rval;
  1688. }
  1689.  
  1690. /* Functions to use in breadth first searches.  */
  1691. typedef tree (*pft)();
  1692. typedef int (*pfi)();
  1693.  
  1694. int tree_needs_constructor_p (type, i)
  1695.      tree type;
  1696. {
  1697.   tree basetype = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  1698.   return TYPE_NEEDS_CONSTRUCTOR (basetype);
  1699. }
  1700.  
  1701. static tree declarator;
  1702.  
  1703. static tree
  1704. get_virtuals_named_this (type, i)
  1705.      tree type;
  1706.      int i;
  1707. {
  1708.   tree basetype = i == 0? type : CLASSTYPE_BASECLASS (type, i);
  1709.   tree fields = lookup_fnfields (CLASSTYPE_AS_LIST (basetype), declarator, 0);
  1710.  
  1711.   if (fields == 0 || fields == error_mark_node)
  1712.     return 0;
  1713.  
  1714.   /* Get to the function decls, and return the first virtual function
  1715.      with this name, if there is one.  */
  1716.   while (fields)
  1717.     {
  1718.       tree fndecl;
  1719.  
  1720.       for (fndecl = TREE_VALUE (fields); fndecl; fndecl = TREE_CHAIN (fndecl))
  1721.     if (DECL_VIRTUAL_P (fndecl))
  1722.       return fields;
  1723.       fields = next_baselink (fields);
  1724.     }
  1725.   return NULL_TREE;
  1726. }
  1727.  
  1728. static tree get_virtual_destructor (type, i)
  1729.      tree type;
  1730.      int i;
  1731. {
  1732.   type = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  1733.   if (TYPE_HAS_DESTRUCTOR (type)
  1734.       && DECL_VIRTUAL_P (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0)))
  1735.     return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
  1736.   return 0;
  1737. }
  1738.  
  1739. int tree_has_any_destructor_p (type, i)
  1740.      tree type;
  1741.      int i;
  1742. {
  1743.   if (i == 0)
  1744.     return TYPE_NEEDS_DESTRUCTOR (type);
  1745.   return TYPE_NEEDS_DESTRUCTOR (CLASSTYPE_BASECLASS (type, i));
  1746. }
  1747.  
  1748. /* Given a class type TYPE, and a function decl FNDECL,
  1749.    look for the first function the TYPE's heirarchy which
  1750.    FNDECL could match as a virtual function.
  1751.  
  1752.    DTORP is nonzero if we are looking for a destructor.  Destructors
  1753.    need special treatment because they do not match by name.  */
  1754. tree
  1755. get_first_matching_virtual (type, fndecl, dtorp)
  1756.      tree type, fndecl;
  1757.      int dtorp;
  1758. {
  1759.   tree tmp = NULL_TREE;
  1760.  
  1761.   /* Breadth first search routines start searching basetypes
  1762.      of TYPE, so we must perform first ply of search here.  */
  1763.   if (dtorp)
  1764.     {
  1765.       if (tree_has_any_destructor_p (type, 0))
  1766.     tmp = get_virtual_destructor (type, 0);
  1767.  
  1768.       if (tmp)
  1769.     return tmp;
  1770.  
  1771.       tmp = (tree) breadth_first_search (type,
  1772.                      (pfi) get_virtual_destructor,
  1773.                      tree_has_any_destructor_p);
  1774.       return tmp;
  1775.     }
  1776.   else
  1777.     {
  1778.       tree drettype, dtypes, btypes, instptr_type;
  1779.       tree basetype = TYPE_METHOD_BASETYPE (fndecl);
  1780.       tree baselink, best = NULL_TREE;
  1781.       tree name = DECL_NAME (fndecl);
  1782.  
  1783.       declarator = DECL_ORIGINAL_NAME (fndecl);
  1784.       if (DECL_VIRTUAL_P (declarator) == 0)
  1785.     return 0;
  1786.  
  1787.       drettype = TREE_TYPE (TREE_TYPE (fndecl));
  1788.       dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
  1789.       if (DECL_STATIC_FUNCTION_P (fndecl))
  1790.     instptr_type = NULL_TREE;
  1791.       else
  1792.     instptr_type = TREE_TYPE (TREE_VALUE (dtypes));
  1793.  
  1794.       for (baselink = get_virtuals_named_this (type, 0);
  1795.        baselink; baselink = next_baselink (baselink))
  1796.     {
  1797.       for (tmp = TREE_VALUE (baselink); tmp; tmp = TREE_CHAIN (tmp))
  1798.         {
  1799.           if (! DECL_VIRTUAL_P (tmp))
  1800.         continue;
  1801.  
  1802.           btypes = TYPE_ARG_TYPES (TREE_TYPE (tmp));
  1803.           if (instptr_type == NULL_TREE
  1804.           && compparms (TREE_CHAIN (btypes), dtypes, 3))
  1805.         /* Caller knows to give error in this case.  */
  1806.         return tmp;
  1807.  
  1808.           if ((TREE_READONLY (TREE_TYPE (TREE_VALUE (btypes)))
  1809.            == TREE_READONLY (instptr_type))
  1810.           && compparms (TREE_CHAIN (btypes), TREE_CHAIN (dtypes), 3))
  1811.         {
  1812.           if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE
  1813.               && ! comptypes (TREE_TYPE (TREE_TYPE (tmp)), drettype, 1))
  1814.             {
  1815.               error_with_decl (fndecl, "conflicting return type specified for virtual function `%s'");
  1816.               SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
  1817.             }
  1818.           break;
  1819.         }
  1820.         }
  1821.       if (tmp)
  1822.         {
  1823.           /* If this is ambiguous, we will warn about it later.  */
  1824.           if (best)
  1825.         {
  1826.           if (get_base_distance (TYPE_METHOD_BASETYPE (TREE_TYPE (best)),
  1827.                      TYPE_METHOD_BASETYPE (TREE_TYPE (tmp)), 0, 0) > 0)
  1828.             best = tmp;
  1829.         }
  1830.           else
  1831.         best = tmp;
  1832.         }
  1833.     }
  1834.       if (IDENTIFIER_ERROR_LOCUS (name) == NULL_TREE
  1835.       && best == NULL_TREE && warn_overloaded_virtual)
  1836.     {
  1837.       error_with_decl (fndecl, "conficting specification deriving virtual function `%s'");
  1838.       SET_IDENTIFIER_ERROR_LOCUS (name, basetype);
  1839.     }
  1840.       return best;
  1841.     }
  1842. }
  1843.  
  1844. /* Return the list of virtual functions which are abstract in type TYPE.
  1845.    This information is cached, and so must be built on a
  1846.    non-temporary obstack.  */
  1847. tree
  1848. get_abstract_virtuals (type)
  1849.      tree type;
  1850. {
  1851.   /* For each layer of base class (i.e., the first base class, and each
  1852.      virtual base class from that one), modify the virtual function table
  1853.      of the derived class to contain the new virtual function.
  1854.      A class has as many vfields as it has virtual base classes (total).  */
  1855.   tree vfields, vbases, base, tmp;
  1856.   tree vfield = CLASSTYPE_VFIELD (type);
  1857.   tree fcontext = vfield ? DECL_FCONTEXT (vfield) : NULL_TREE;
  1858.   tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (type);
  1859.  
  1860.   for (vfields = CLASSTYPE_VFIELDS (type); vfields; vfields = TREE_CHAIN (vfields))
  1861.     {
  1862.       int normal;
  1863.  
  1864.       /* Find the right base class for this derived class, call it BASE.  */
  1865.       base = TREE_VALUE (vfields);
  1866.       if (base == type)
  1867.     continue;
  1868.  
  1869.       /* We call this case NORMAL iff this virtual function table
  1870.      pointer field has its storage reserved in this class.
  1871.      This is normally the case without virtual baseclasses
  1872.      or off-center multiple baseclasses.  */
  1873.       normal = (base == fcontext
  1874.         && (TREE_PURPOSE (vfields) == NULL_TREE
  1875.             || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vfields))));
  1876.  
  1877.       if (normal)
  1878.     tmp = TREE_CHAIN (CLASS_ASSOC_VIRTUALS (type));
  1879.       else
  1880.     {
  1881.       tree assoc = assoc_value (base, type);
  1882.       tmp = TREE_CHAIN (ASSOC_VIRTUALS (assoc));
  1883.     }
  1884.  
  1885.       while (tmp)
  1886.     {
  1887.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
  1888.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  1889.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  1890.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  1891.       tmp = TREE_CHAIN (tmp);
  1892.     }
  1893.     }
  1894.   for (vbases = CLASSTYPE_VBASECLASSES (type); vbases; vbases = TREE_CHAIN (vbases))
  1895.     {
  1896.       if (! ASSOC_VIRTUALS (vbases));
  1897.     continue;
  1898.  
  1899.       base = TREE_TYPE (vbases);
  1900.       tmp = TREE_CHAIN (ASSOC_VIRTUALS (vbases));
  1901.       while (tmp)
  1902.     {
  1903.       tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (tmp));
  1904.       tree base_fndecl = TREE_OPERAND (base_pfn, 0);
  1905.       if (DECL_ABSTRACT_VIRTUAL_P (base_fndecl))
  1906.         abstract_virtuals = tree_cons (NULL_TREE, base_fndecl, abstract_virtuals);
  1907.       tmp = TREE_CHAIN (tmp);
  1908.     }
  1909.     }
  1910.   return nreverse (abstract_virtuals);
  1911. }
  1912.  
  1913. /* For the type TYPE, return a list of member functions available from
  1914.    base classes with name NAME.  The TREE_VALUE of the list is a chain of
  1915.    member functions with name NAME.  The TREE_PURPOSE of the list is a
  1916.    basetype, or a list of base types (in reverse order) which were
  1917.    traversed to reach the chain of member functions.  If we reach a base
  1918.    type which provides a member function of name NAME, and which has at
  1919.    most one base type itself, then we can terminate the search.  */
  1920.  
  1921. tree
  1922. get_baselinks (type, name)
  1923.      tree type, name;
  1924. {
  1925.   tree hash_tree_cons ();
  1926.   int head = 0, tail = 0, index;
  1927.   tree rval = 0, nval = 0;
  1928.   tree basetypes = CLASSTYPE_AS_LIST (type);
  1929.  
  1930.   search_stack = push_search_level (search_stack, &search_obstack);
  1931.  
  1932.   while (1)
  1933.     {
  1934.       int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  1935.  
  1936.       /* Process and/or queue base types.  */
  1937.       for (i = 1; i <= n_baselinks; i++)
  1938.     if (CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0)
  1939.       {
  1940.         tree btypes;
  1941.  
  1942.         btypes = hash_tree_cons (CLASSTYPE_VIA_PUBLIC (type, i),
  1943.                      CLASSTYPE_VIA_VIRTUAL (type, i),
  1944.                      NULL_TREE, CLASSTYPE_BASECLASS (type, i),
  1945.                      basetypes);
  1946.         CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) = 1;
  1947.         obstack_ptr_grow (&search_obstack, btypes);
  1948.  
  1949.         tail += 1;
  1950.         if (tail >= search_stack->limit)
  1951.           abort ();
  1952.       }
  1953.  
  1954.     dont_queue:
  1955.       /* Process head of queue, if one exists.  */
  1956.       if (head >= tail)
  1957.     break;
  1958.  
  1959.       basetypes = search_stack->first[head++];
  1960.       type = TREE_VALUE (basetypes);
  1961.       index = lookup_fnfields_1 (type, name);
  1962.       if (index >= 0)
  1963.     {
  1964.       nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), index);
  1965.       rval = hash_tree_cons (0, 0, basetypes, nval, rval);
  1966.       if (CLASSTYPE_N_BASECLASSES (type) <= 1)
  1967.         {
  1968.           if (CLASSTYPE_BASELINK_VEC (type))
  1969.         TREE_TYPE (rval) = TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), index);
  1970.           goto dont_queue;
  1971.         }
  1972.     }
  1973.       nval = NULL_TREE;
  1974.     }
  1975.   {
  1976.     tree *tp = search_stack->first;
  1977.     tree *search_tail = tp + tail;
  1978.  
  1979.     while (tp < search_tail)
  1980.       {
  1981.     CLASSTYPE_MARKED (TREE_VALUE (*tp++)) = 0;
  1982.       }
  1983.   }
  1984.   search_stack = pop_search_level (search_stack);
  1985.   return rval;
  1986. }
  1987.  
  1988. tree
  1989. next_baselink (baselink)
  1990.      tree baselink;
  1991. {
  1992.   tree tmp = TREE_TYPE (baselink);
  1993.   baselink = TREE_CHAIN (baselink);
  1994.   while (tmp)
  1995.     {
  1996.       /* @@ does not yet add previous base types.  */
  1997.       baselink = tree_cons (TREE_PURPOSE (tmp), TREE_VALUE (tmp),
  1998.                 baselink);
  1999.       TREE_TYPE (baselink) = TREE_TYPE (tmp);
  2000.       tmp = TREE_CHAIN (tmp);
  2001.     }
  2002.   return baselink;
  2003. }
  2004.  
  2005. /* DEPTH-FIRST SEARCH ROUTINES.  */
  2006.  
  2007. /* Assign unique numbers to _CLASSTYPE members of the lattice
  2008.    specified by TYPE.  The root nodes are marked first; the nodes
  2009.    are marked depth-fisrt, left-right.  */
  2010.  
  2011. static int cid;
  2012.  
  2013. /* Matrix implementing a relation from CLASSTYPE X CLASSTYPE => INT.
  2014.    Relation yields 1 if C1 <= C2, 0 otherwise.  */
  2015. typedef char mi_boolean;
  2016. static mi_boolean *mi_matrix;
  2017.  
  2018. /* Type for which this matrix is defined.  */
  2019. static tree mi_type;
  2020.  
  2021. /* Size of the matrix for indexing purposes.  */
  2022. static int mi_size;
  2023.  
  2024. /* Return nonzero if class C2 derives from class C1.  */
  2025. #define DERIVES_FROM(C1, C2)    \
  2026.   ((mi_matrix+mi_size*(CLASSTYPE_CID (C1)-1))[CLASSTYPE_CID (C2)-1])
  2027. #define DERIVES_FROM_STAR(C)    \
  2028.   (mi_matrix+(CLASSTYPE_CID (C)-1))
  2029.  
  2030. /* The main function which implements depth first search.  */
  2031. static void
  2032. dfs_walk (type, fn, qfn)
  2033.      tree type;
  2034.      void (*fn)();
  2035.      int (*qfn)();
  2036. {
  2037.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2038.  
  2039.   for (i = 1; i <= n_baselinks; i++)
  2040.     if ((*qfn)(CLASSTYPE_BASECLASS (type, i)))
  2041.       {
  2042.     dfs_walk (CLASSTYPE_BASECLASS (type, i), fn, qfn);
  2043.       }
  2044.  
  2045.   fn (type);
  2046. }
  2047.  
  2048. /* Predicate functions which serve for dfs_walk.  */
  2049. static int numberedp (type) tree type;
  2050. { return CLASSTYPE_CID (type); }
  2051. static int unnumberedp (type) tree type;
  2052. { return CLASSTYPE_CID (type) == 0; }
  2053.  
  2054. static int markedp (type) tree type;
  2055. { return CLASSTYPE_MARKED (type); }
  2056. static int bfs_markedp (type, i) tree type; int i;
  2057. { return CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)); }
  2058. static int unmarkedp (type) tree type;
  2059. { return CLASSTYPE_MARKED (type) == 0; }
  2060. static int bfs_unmarkedp (type, i) tree type; int i;
  2061. { return CLASSTYPE_MARKED (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2062. static int marked2p (type) tree type;
  2063. { return CLASSTYPE_MARKED2 (type); }
  2064. static int bfs_marked2p (type, i) tree type; int i;
  2065. { return CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)); }
  2066. static int unmarked2p (type) tree type;
  2067. { return CLASSTYPE_MARKED2 (type) == 0; }
  2068. static int bfs_unmarked2p (type, i) tree type; int i;
  2069. { return CLASSTYPE_MARKED2 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2070. static int marked3p (type) tree type;
  2071. { return CLASSTYPE_MARKED3 (type); }
  2072. static int bfs_marked3p (type, i) tree type; int i;
  2073. { return CLASSTYPE_MARKED3 (CLASSTYPE_BASECLASS (type, i)); }
  2074. static int unmarked3p (type) tree type;
  2075. { return CLASSTYPE_MARKED3 (type) == 0; }
  2076. static int bfs_unmarked3p (type, i) tree type; int i;
  2077. { return CLASSTYPE_MARKED3 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2078. static int marked4p (type) tree type;
  2079. { return CLASSTYPE_MARKED4 (type); }
  2080. static int bfs_marked4p (type, i) tree type; int i;
  2081. { return CLASSTYPE_MARKED4 (CLASSTYPE_BASECLASS (type, i)); }
  2082. static int unmarked4p (type) tree type;
  2083. { return CLASSTYPE_MARKED4 (type) == 0; }
  2084. static int bfs_unmarked4p (type, i) tree type; int i;
  2085. { return CLASSTYPE_MARKED4 (CLASSTYPE_BASECLASS (type, i)) == 0; }
  2086.  
  2087. static int dfs_search_slot_nonempty_p (type) tree type;
  2088. { return CLASSTYPE_SEARCH_SLOT (type) != 0; }
  2089.  
  2090.  
  2091. /* The worker functions for `dfs_walk'.  These do not need to
  2092.    test anything (vis a vis marking) if they are paired with
  2093.    a predicate function (above).  */
  2094.  
  2095. /* Assign each type within the lattice a number which is unique
  2096.    in the lattice.  The first number assigned is 1.  */
  2097.  
  2098. static void
  2099. dfs_number (type)
  2100.      tree type;
  2101. {
  2102.   CLASSTYPE_CID (type) = ++cid;
  2103. }
  2104.  
  2105. static void
  2106. dfs_unnumber (type)
  2107.      tree type;
  2108. {
  2109.   CLASSTYPE_CID (type) = 0;
  2110. }
  2111.  
  2112. static void
  2113. dfs_mark (type) tree type;
  2114. { CLASSTYPE_MARKED (type) = 1; }
  2115.  
  2116. static void
  2117. dfs_unmark (type) tree type;
  2118. { CLASSTYPE_MARKED (type) = 0; }
  2119.  
  2120. static void
  2121. dfs_mark2 (type) tree type;
  2122. { CLASSTYPE_MARKED2 (type) = 1; }
  2123.  
  2124. static void
  2125. dfs_unmark2 (type) tree type;
  2126. { CLASSTYPE_MARKED2 (type) = 0; }
  2127.  
  2128. static void
  2129. dfs_mark3 (type) tree type;
  2130. { CLASSTYPE_MARKED3 (type) = 1; }
  2131.  
  2132. static void
  2133. dfs_unmark3 (type) tree type;
  2134. { CLASSTYPE_MARKED3 (type) = 0; }
  2135.  
  2136. static void
  2137. dfs_mark4 (type) tree type;
  2138. { CLASSTYPE_MARKED4 (type) = 1; }
  2139.  
  2140. static void
  2141. dfs_unmark4 (type) tree type;
  2142. { CLASSTYPE_MARKED4 (type) = 0; }
  2143.  
  2144. static void
  2145. dfs_unmark12 (type) tree type;
  2146. { CLASSTYPE_MARKED (type) = 0;
  2147.   CLASSTYPE_MARKED2 (type) = 0; }
  2148.  
  2149. static void
  2150. dfs_unmark34 (type) tree type;
  2151. { CLASSTYPE_MARKED3 (type) = 0;
  2152.   CLASSTYPE_MARKED4 (type) = 0; }
  2153.  
  2154. static tree
  2155. dfs_clear_search_slot (type) tree type;
  2156. { CLASSTYPE_SEARCH_SLOT (type) = 0; }
  2157.  
  2158. static tree vbase_types;
  2159. static tree vbase_decl, vbase_decl_ptr;
  2160. static tree vbase_init_result;
  2161.  
  2162. static void
  2163. dfs_find_vbases (type)
  2164.      tree type;
  2165. {
  2166.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2167.  
  2168.   for (i = n_baselinks; i > 0; i--)
  2169.     if (CLASSTYPE_VIA_VIRTUAL (type, i)
  2170.     && CLASSTYPE_SEARCH_SLOT (CLASSTYPE_BASECLASS (type, i)) == 0)
  2171.       {
  2172.     tree vbase = CLASSTYPE_BASECLASS (type, i);
  2173.     /* ??? ASSOC_VALUE and TREE_VALUE must be the same for this to work.  */
  2174.     tree assoc = value_member (TYPE_MAIN_VARIANT (vbase), vbase_types);
  2175.  
  2176.     CLASSTYPE_SEARCH_SLOT (vbase)
  2177.       = build (PLUS_EXPR, TYPE_POINTER_TO (vbase),
  2178.            vbase_decl_ptr, ASSOC_OFFSET (assoc));
  2179.       }
  2180.   CLASSTYPE_MARKED3 (type) = 1;
  2181.   CLASSTYPE_MARKED4 (type) = 1;
  2182. }
  2183.  
  2184. static void
  2185. dfs_init_vbase_pointers (type)
  2186.      tree type;
  2187. {
  2188.   tree fields = TYPE_FIELDS (type);
  2189.   tree path, this_vbase_ptr;
  2190.   int distance;
  2191.  
  2192.   CLASSTYPE_MARKED3 (type) = 0;
  2193.  
  2194.   if (fields == NULL_TREE
  2195.       || DECL_NAME (fields) == NULL_TREE
  2196.       || ! VBASE_NAME_P (DECL_NAME (fields)))
  2197.     return;
  2198.  
  2199.   distance = get_base_distance (type, TREE_TYPE (vbase_decl), 0, &path);
  2200.   while (path)
  2201.     {
  2202.       if (TREE_VIA_VIRTUAL (path))
  2203.     break;
  2204.       distance -= 1;
  2205.       path = TREE_CHAIN (path);
  2206.     }
  2207.  
  2208.   if (distance > 0)
  2209.     this_vbase_ptr = convert_pointer_to (type, CLASSTYPE_SEARCH_SLOT (TREE_VALUE (path)));
  2210.   else
  2211.     this_vbase_ptr = convert_pointer_to (type, vbase_decl_ptr);
  2212.  
  2213.   while (fields && DECL_NAME (fields)
  2214.      && VBASE_NAME_P (DECL_NAME (fields)))
  2215.     {
  2216.       tree ref = build (COMPONENT_REF, TREE_TYPE (fields),
  2217.             build_indirect_ref (this_vbase_ptr, 0), fields);
  2218.       tree init = CLASSTYPE_SEARCH_SLOT (TREE_TYPE (TREE_TYPE (fields)));
  2219.       vbase_init_result = tree_cons (TREE_TYPE (TREE_TYPE (fields)),
  2220.                      build_modify_expr (ref, NOP_EXPR, init),
  2221.                      vbase_init_result);
  2222.       fields = TREE_CHAIN (fields);
  2223.     }
  2224. }
  2225.  
  2226. /* Sometimes this needs to clear both 3 and 4.  Other times,
  2227.    just 4, but optimizer should make both with equal efficiency
  2228.    (though it does not currently).  */
  2229. static void
  2230. dfs_clear_vbase_slots (type)
  2231.      tree type;
  2232. {
  2233.   CLASSTYPE_SEARCH_SLOT (type) = 0;
  2234.   CLASSTYPE_MARKED3 (type) = 0;
  2235.   CLASSTYPE_MARKED4 (type) = 0;
  2236. }
  2237.  
  2238. tree
  2239. init_vbase_pointers (type, decl_ptr)
  2240.      tree type;
  2241.      tree decl_ptr;
  2242. {
  2243.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2244.     {
  2245.       int old_flag = flag_this_is_variable;
  2246.       flag_this_is_variable = 0;
  2247.       vbase_types = CLASSTYPE_VBASECLASSES (type);
  2248.       vbase_decl_ptr = decl_ptr;
  2249.       vbase_decl = build_indirect_ref (decl_ptr, 0);
  2250.       vbase_init_result = NULL_TREE;
  2251.       dfs_walk (type, dfs_find_vbases, unmarked3p);
  2252.       dfs_walk (type, dfs_init_vbase_pointers, marked3p);
  2253.       dfs_walk (type, dfs_clear_vbase_slots, marked4p);
  2254.       flag_this_is_variable = old_flag;
  2255.       return vbase_init_result;
  2256.     }
  2257.   return 0;
  2258. }
  2259.  
  2260. /* Build a COMPOUND_EXPR which when expanded will generate the code
  2261.    needed to initialize all the virtual function table slots of all
  2262.    the virtual baseclasses.  FOR_TYPE is the type which determines the
  2263.    virtual baseclasses to use; TYPE is the type of the object to which
  2264.    the initialization applies.  TRUE_EXP is the true object we are
  2265.    initializing, and DECL_PTR is the pointer to the sub-object we
  2266.    are initializing.
  2267.  
  2268.    CTOR_P is non-zero if the caller of this function is a top-level
  2269.    constructor.  It is zero when called from a destructor.  When
  2270.    non-zero, we can use computed offsets to store the vtables.  When
  2271.    zero, we must store new vtables through virtual baseclass pointers.  */
  2272.  
  2273. tree
  2274. build_vbase_vtables_init (for_type, type, true_exp, decl_ptr, ctor_p)
  2275.      tree for_type, type;
  2276.      tree true_exp, decl_ptr;
  2277.      int ctor_p;
  2278. {
  2279.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  2280.     {
  2281.       int old_flag = flag_this_is_variable;
  2282.       tree vtable_init_result = NULL_TREE;
  2283.       tree vbases = CLASSTYPE_VBASECLASSES (type);
  2284.  
  2285.       vbase_types = CLASSTYPE_VBASECLASSES (for_type);
  2286.       vbase_decl_ptr = true_exp ? build_unary_op (ADDR_EXPR, true_exp, 0) : decl_ptr;
  2287.       vbase_decl = true_exp ? true_exp : build_indirect_ref (decl_ptr, 0);
  2288.       flag_this_is_variable = 0;
  2289.  
  2290.       if (ctor_p)
  2291.     /* This is an object of type IN_TYPE,  */
  2292.     dfs_walk (for_type, dfs_find_vbases, unmarked4p);
  2293.  
  2294.       /* Initialized with vtables of type TYPE.  */
  2295.       while (vbases)
  2296.     {
  2297.       tree basetype = get_base_type (ASSOC_VALUE (vbases), type, 0);
  2298.  
  2299.       /* This time through, not every class's vtable
  2300.          is going to be initialized.  That is, we only initialize
  2301.          the "last" vtable pointer.  */
  2302.  
  2303.       assert (basetype == ASSOC_TYPE (vbases));
  2304.  
  2305.       if (basetype
  2306.           && CLASSTYPE_VSIZE (basetype)
  2307.           && TYPE_MAIN_VARIANT (basetype) == ASSOC_VALUE (vbases))
  2308.         {
  2309.           tree addr;
  2310.           tree vtbl = ASSOC_VTABLE (vbases);
  2311.           tree init = build_unary_op (ADDR_EXPR, vtbl, 0);
  2312.           TREE_USED (vtbl) = 1;
  2313.  
  2314.           if (ctor_p == 0)
  2315.         addr = convert_pointer_to (basetype, vbase_decl_ptr);
  2316.           else
  2317.         addr = CLASSTYPE_SEARCH_SLOT (basetype);
  2318.  
  2319.           if (addr)
  2320.         {
  2321.           tree ref = build_vfield_ref (build_indirect_ref (addr, 0), basetype);
  2322.           init = convert_force (TREE_TYPE (ref), init);
  2323.           vtable_init_result = tree_cons (NULL_TREE, build_modify_expr (ref, NOP_EXPR, init),
  2324.                           vtable_init_result);
  2325.         }
  2326.         }
  2327.       vbases = TREE_CHAIN (vbases);
  2328.     }
  2329.  
  2330.       dfs_walk (type, dfs_clear_vbase_slots, marked4p);
  2331.  
  2332.       flag_this_is_variable = old_flag;
  2333.       if (vtable_init_result)
  2334.     return build_compound_expr (vtable_init_result);
  2335.     }
  2336.   return error_mark_node;
  2337. }
  2338.  
  2339. tree
  2340. clear_search_slots (type)
  2341.      tree type;
  2342. {
  2343.   dfs_walk (type, dfs_clear_search_slot, dfs_search_slot_nonempty_p);
  2344. }
  2345.  
  2346. static void
  2347. dfs_get_vbase_types (type)
  2348.      tree type;
  2349. {
  2350.   int i;
  2351.   tree these_vbase_types = CLASSTYPE_VBASECLASSES (type);
  2352.   tree basetype;
  2353.  
  2354.   if (these_vbase_types)
  2355.     {
  2356.       while (these_vbase_types)
  2357.     {
  2358.       basetype = ASSOC_TYPE (these_vbase_types);
  2359.       if (! CLASSTYPE_MARKED2 (basetype))
  2360.         {
  2361.           vbase_types = make_assoc (integer_zero_node,
  2362.                     basetype,
  2363.                     CLASS_ASSOC_VTABLE (basetype),
  2364.                     CLASS_ASSOC_VIRTUALS (basetype),
  2365.                     vbase_types);
  2366.           CLASSTYPE_MARKED2 (basetype) = 1;
  2367.         }
  2368.       these_vbase_types = TREE_CHAIN (these_vbase_types);
  2369.     }
  2370.     }
  2371.   else for (i = CLASSTYPE_N_BASECLASSES (type); i > 0; i--)
  2372.     {
  2373.       basetype = CLASSTYPE_BASECLASS (type, i);
  2374.       if (CLASSTYPE_VIA_VIRTUAL (type, i) && ! CLASSTYPE_MARKED2 (basetype))
  2375.     {
  2376.       vbase_types = make_assoc (integer_zero_node,
  2377.                     basetype,
  2378.                     CLASS_ASSOC_VTABLE (basetype),
  2379.                     CLASS_ASSOC_VIRTUALS (basetype),
  2380.                     vbase_types);
  2381.       CLASSTYPE_MARKED2 (basetype) = 1;
  2382.     }
  2383.     }
  2384.   CLASSTYPE_MARKED (type) = 1;
  2385. }
  2386.  
  2387. /* Some virtual baseclasses might be virtual baseclasses for
  2388.    other virtual baseclasses.  We sort the virtual baseclasses
  2389.    topologically: in the list returned, the first virtual base
  2390.    classes have no virtual baseclasses themselves, and any entry
  2391.    on the list has no dependency on virtual base classes later in the
  2392.    list.  */
  2393. tree
  2394. get_vbase_types (type)
  2395.      tree type;
  2396. {
  2397.   tree ordered_vbase_types = NULL_TREE, prev, next;
  2398.   tree vbases;
  2399.  
  2400.   vbase_types = NULL_TREE;
  2401.   dfs_walk (type, dfs_get_vbase_types, unmarkedp);
  2402.   dfs_walk (type, dfs_unmark, markedp);
  2403.  
  2404.   while (vbase_types)
  2405.     {
  2406.       /* Now sort these types.  This is essentially a bubble merge.  */
  2407.  
  2408.       /* Farm out virtual baseclasses which have no marked ancestors.  */
  2409.       for (vbases = vbase_types, prev = NULL_TREE;
  2410.        vbases; vbases = next)
  2411.     {
  2412.       next = TREE_CHAIN (vbases);
  2413.       if (! TYPE_USES_VIRTUAL_BASECLASSES (ASSOC_TYPE (vbases))
  2414.           || CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) == 0)
  2415.         {
  2416.           if (prev)
  2417.         TREE_CHAIN (prev) = TREE_CHAIN (vbases);
  2418.           else
  2419.         vbase_types = TREE_CHAIN (vbases);
  2420.           TREE_CHAIN (vbases) = NULL_TREE;
  2421.           ordered_vbase_types = chainon (ordered_vbase_types, vbases);
  2422.           CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) = 0;
  2423.         }
  2424.       else
  2425.         prev = vbases;
  2426.     }
  2427.  
  2428.       /* Now unmark types all of whose ancestors are now on the
  2429.      `ordered_vbase_types' list.  */
  2430.       for (vbases = vbase_types; vbases; vbases = TREE_CHAIN (vbases))
  2431.     {
  2432.       /* If all our virtual baseclasses are unmarked, ok.  */
  2433.       tree t = CLASSTYPE_VBASECLASSES (ASSOC_VALUE (vbases));
  2434.       while (t && (CLASSTYPE_MARKED2 (ASSOC_TYPE (t)) == 0
  2435.                || !TYPE_USES_VIRTUAL_BASECLASSES (ASSOC_TYPE (t))))
  2436.         t = TREE_CHAIN (t);
  2437.       if (t == NULL_TREE)
  2438.         CLASSTYPE_MARKED2 (ASSOC_TYPE (vbases)) = 0;
  2439.     }
  2440.     }
  2441.  
  2442.   return ordered_vbase_types;
  2443. }
  2444.  
  2445. static void
  2446. dfs_record_inheritance (type)
  2447.      tree type;
  2448. {
  2449.   int i, n_baselinks = CLASSTYPE_N_BASECLASSES (type);
  2450.   mi_boolean *derived_row = DERIVES_FROM_STAR (type);
  2451.  
  2452.   for (i = n_baselinks; i > 0; i--)
  2453.     {
  2454.       int j;
  2455.       tree baseclass = TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (type, i));
  2456.       mi_boolean *base_row = DERIVES_FROM_STAR (baseclass);
  2457.  
  2458.       /* Don't search if there's nothing there!  MI_SIZE can be
  2459.      zero as a result of parse errors.  */
  2460.       if (CLASSTYPE_N_BASECLASSES (baseclass) && mi_size > 0)
  2461.     for (j = mi_size*(CLASSTYPE_CID (baseclass)-1); j >= 0; j -= mi_size)
  2462.       derived_row[j] |= base_row[j];
  2463.       DERIVES_FROM (baseclass, type) = 1;
  2464.     }
  2465.  
  2466.   CLASSTYPE_MARKED (type) = 1;
  2467. }
  2468.  
  2469. /* Given a _CLASSTYPE node in a multiple inheritance lattice,
  2470.    convert the lattice into a simple relation such that,
  2471.    given to CIDs, C1 and C2, one can determine if C1 <= C2
  2472.    or C2 <= C1 or C1 <> C2.
  2473.  
  2474.    Once constructed, we walk the lattice depth fisrt,
  2475.    applying various functions to elements as they are encountered.
  2476.  
  2477.    We use malloc here, in case we want to randomly free these tables.  */
  2478.  
  2479. #define SAVE_MI_MATRIX
  2480.  
  2481. void
  2482. build_mi_matrix (type)
  2483.      tree type;
  2484. {
  2485.   cid = 0;
  2486.  
  2487. #ifdef SAVE_MI_MATRIX
  2488.   if (CLASSTYPE_MI_MATRIX (type))
  2489.     {
  2490.       mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  2491.       mi_matrix = CLASSTYPE_MI_MATRIX (type);
  2492.       mi_type = type;
  2493.       dfs_walk (type, dfs_number, unnumberedp);
  2494.       return;
  2495.     }
  2496. #endif
  2497.  
  2498.   mi_size = CLASSTYPE_N_SUPERCLASSES (type) + CLASSTYPE_N_VBASECLASSES (type);
  2499.   mi_matrix = (char *)malloc ((mi_size+1) * (mi_size+1));
  2500.   mi_type = type;
  2501.   bzero (mi_matrix, mi_size * mi_size);
  2502.   dfs_walk (type, dfs_number, unnumberedp);
  2503.   dfs_walk (type, dfs_record_inheritance, unmarkedp);
  2504.   dfs_walk (type, dfs_unmark, markedp);
  2505. }
  2506.  
  2507. void
  2508. free_mi_matrix ()
  2509. {
  2510.   dfs_walk (mi_type, dfs_unnumber, numberedp);
  2511.  
  2512. #ifdef SAVE_MI_MATRIX
  2513.   CLASSTYPE_MI_MATRIX (mi_type) = mi_matrix;
  2514. #else
  2515.   free (mi_matrix);
  2516.   mi_size = 0;
  2517.   cid = 0;
  2518. #endif
  2519. }
  2520.  
  2521. /* Local variables for detecting ambiguities of virtual functions
  2522.    when two or more classes are joined at a multiple inheritance
  2523.    seam.  */
  2524. typedef tree mi_ventry[3];
  2525. static mi_ventry *mi_vmatrix;
  2526. static int *mi_vmax;
  2527. static int mi_vrows, mi_vcols;
  2528. #define MI_VMATRIX(ROW,COL) ((mi_vmatrix + (ROW)*mi_vcols)[COL])
  2529.  
  2530. /* Build a table of virtual functions for a multiple-inheritance
  2531.    structure.  Here, there are N base classes, and at most
  2532.    M entries per class.
  2533.  
  2534.    This function does nothing if N is 0 or 1.  */
  2535. void
  2536. build_mi_virtuals (rows, cols)
  2537.      int rows, cols;
  2538. {
  2539.   if (rows < 2)
  2540.     return;
  2541.   mi_vrows = rows;
  2542.   mi_vcols = cols;
  2543.   mi_vmatrix = (mi_ventry *)malloc ((rows+1) * cols * sizeof (mi_ventry));
  2544.   mi_vmax = (int *)malloc ((rows+1) * sizeof (int));
  2545.  
  2546.   bzero (mi_vmax, rows * sizeof (int));
  2547.  
  2548.   /* Row indicies start at 1, so adjust this.  */
  2549.   mi_vmatrix -= cols;
  2550.   mi_vmax -= 1;
  2551. }
  2552.  
  2553. /* Comparison function for ordering virtual function table entries.  */
  2554. static int
  2555. rank_mi_virtuals (v1, v2)
  2556.      mi_ventry *v1, *v2;
  2557. {
  2558.   tree p1, p2;
  2559.   int i;
  2560.  
  2561.   i = (TREE_UID (DECL_ORIGINAL_NAME ((*v1)[0]))
  2562.        - TREE_UID (DECL_ORIGINAL_NAME ((*v2)[0])));
  2563.   if (i)
  2564.     return i;
  2565.   p1 = (*v1)[1];
  2566.   p2 = (*v2)[1];
  2567.  
  2568.   if (p1 == p2)
  2569.     return 0;
  2570.  
  2571.   while (p1 && p2)
  2572.     {
  2573.       i = (TREE_UID (TREE_VALUE (p1))
  2574.        - TREE_UID (TREE_VALUE (p2)));
  2575.       if (i)
  2576.     return i;
  2577.  
  2578.       if (TREE_CHAIN (p1))
  2579.     {
  2580.       if (! TREE_CHAIN (p2))
  2581.         return 1;
  2582.       p1 = TREE_CHAIN (p1);
  2583.       p2 = TREE_CHAIN (p2);
  2584.     }
  2585.       else if (TREE_CHAIN (p2))
  2586.     return -1;
  2587.       else
  2588.     {
  2589.       /* When matches of argument lists occur, pick lowest
  2590.          TREE_UID to keep searching time to a minimum on
  2591.          later passes--like hashing, only different.
  2592.          *MUST BE STABLE*.  */
  2593.       if (TREE_UID ((*v2)[1]) < TREE_UID ((*v1)[1]))
  2594.         (*v1)[1] = (*v2)[1];
  2595.       else
  2596.         (*v2)[1] = (*v1)[1];
  2597.       return 0;
  2598.     }
  2599.     }
  2600.   return 0;
  2601. }
  2602.  
  2603. /* Install the virtuals functions got from the initializer VIRTUALS to
  2604.    the table at index ROW.  */
  2605. void
  2606. add_mi_virtuals (row, virtuals)
  2607.      int row;
  2608.      tree virtuals;
  2609. {
  2610.   int col = 0;
  2611.  
  2612.   if (mi_vmatrix == 0)
  2613.     return;
  2614.   while (virtuals)
  2615.     {
  2616.       tree decl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
  2617.       MI_VMATRIX (row, col)[0] = decl;
  2618.       MI_VMATRIX (row, col)[1] = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
  2619.       MI_VMATRIX (row, col)[2] = TREE_VALUE (virtuals);
  2620.       virtuals = TREE_CHAIN (virtuals);
  2621.       col += 1;
  2622.     }
  2623.   mi_vmax[row] = col;
  2624.  
  2625.   qsort (mi_vmatrix + row * mi_vcols,
  2626.      col,
  2627.      sizeof (mi_ventry),
  2628.      rank_mi_virtuals);
  2629. }
  2630.  
  2631. /* If joining two types results in an ambiguity in the virtual
  2632.    function table, report such here.  */
  2633. void
  2634. report_ambiguous_mi_virtuals (rows, type)
  2635.      int rows;
  2636.      tree type;
  2637. {
  2638.   int *mi_vmin;
  2639.   int row1, col1, row, col;
  2640.  
  2641.   if (mi_vmatrix == 0)
  2642.     return;
  2643.  
  2644.   /* Now virtuals are all sorted, so we merge to find ambiguous cases.  */
  2645.   mi_vmin = (int *)alloca ((rows+1) * sizeof (int));
  2646.   bzero (mi_vmin, rows * sizeof (int));
  2647.  
  2648.   /* adjust.  */
  2649.   mi_vmin -= 1;
  2650.  
  2651.   /* For each base class with virtual functions (and this includes views
  2652.      of the virtual baseclasses from different base classes), see that
  2653.      each virtual function in that base class has a unique meet.
  2654.  
  2655.      When the column loop is finished, THIS_DECL is in fact the meet.
  2656.      If that value does not appear in the virtual function table for
  2657.      the row, install it.  This happens when that virtual function comes
  2658.      from a virtual baseclass, or a non-leftmost baseclass.  */
  2659.      
  2660.   for (row1 = 1; row1 < rows; row1++)
  2661.     {
  2662.       tree this_decl = 0;
  2663.  
  2664.       for (col1 = mi_vmax[row1]-1; col1 >= mi_vmin[row1]; col1--)
  2665.     {
  2666.       tree these_args = MI_VMATRIX (row1, col1)[1];
  2667.       tree this_context;
  2668.  
  2669.       this_decl = MI_VMATRIX (row1, col1)[0];
  2670.       if (this_decl == 0)
  2671.         continue;
  2672.       this_context = DECL_CONTEXT (this_decl);
  2673.  
  2674.       if (this_context != type)
  2675.         this_context = get_base_type (this_context, type, 0);
  2676.  
  2677.       for (row = row1+1; row <= rows; row++)
  2678.         for (col = mi_vmax[row]-1; col >= mi_vmin[row]; col--)
  2679.           {
  2680.         mi_ventry this_entry;
  2681.  
  2682.         if (MI_VMATRIX (row, col)[0] == 0)
  2683.           continue;
  2684.  
  2685.         this_entry[0] = this_decl;
  2686.         this_entry[1] = these_args;
  2687.         this_entry[2] = MI_VMATRIX (row1, col1)[2];
  2688.         if (rank_mi_virtuals (&this_entry,
  2689.                       &MI_VMATRIX (row, col)) == 0)
  2690.           {
  2691.             /* They are equal.  There are four possibilities:
  2692.                
  2693.                (1) Derived class is defining this virtual function.
  2694.                (2) Two paths to the same virtual function in the
  2695.                same base class.
  2696.                (3) A path to a virtual function declared in one base
  2697.                class, and another path to a virtual function in a
  2698.                base class of the base class.
  2699.                (4) Two paths to the same virtual function in different
  2700.                base classes.
  2701.                
  2702.                The first three cases are ok (non-ambiguous).  */
  2703.  
  2704.             tree that_context, tmp;
  2705.             int this_before_that;
  2706.  
  2707.             if (type == this_context)
  2708.               /* case 1.  */
  2709.               goto ok;
  2710.             that_context = get_base_type (DECL_CONTEXT (MI_VMATRIX (row, col)[0]), type, 0);
  2711.             if (that_context == this_context)
  2712.               /* case 2.  */
  2713.               goto ok;
  2714.             if (that_context != NULL_TREE)
  2715.               {
  2716.             tmp = get_base_type (that_context, this_context, 0);
  2717.             this_before_that = (that_context != tmp);
  2718.             if (this_before_that == 0)
  2719.               /* case 3a.  */
  2720.               goto ok;
  2721.             tmp = get_base_type (this_context, that_context, 0);
  2722.             this_before_that = (this_context == tmp);
  2723.             if (this_before_that != 0)
  2724.               /* case 3b.  */
  2725.               goto ok;
  2726.  
  2727.             /* case 4.  */
  2728.             error_with_decl (MI_VMATRIX (row, col)[0], "ambiguous virtual function `%s'");
  2729.             error_with_decl (this_decl, "ambiguating function `%s' (joined by type `%s')", IDENTIFIER_POINTER (current_class_name));
  2730.               }
  2731.           ok:
  2732.             MI_VMATRIX (row, col)[0] = 0;
  2733.  
  2734.             /* Let zeros propagate.  */
  2735.             if (col == mi_vmax[row]-1)
  2736.               {
  2737.             int i = col;
  2738.             while (i >= mi_vmin[row]
  2739.                    && MI_VMATRIX (row, i)[0] == 0)
  2740.               i--;
  2741.             mi_vmax[row] = i;
  2742.               }
  2743.             else if (col == mi_vmin[row])
  2744.               {
  2745.             int i = col;
  2746.             while (i < mi_vmax[row]
  2747.                    && MI_VMATRIX (row, i)[0] == 0)
  2748.               i++;
  2749.             mi_vmin[row] = i;
  2750.               }
  2751.           }
  2752.           }
  2753.     }
  2754.     }
  2755.   free (mi_vmatrix + mi_vcols);
  2756.   mi_vmatrix = 0;
  2757.   free (mi_vmax + 1);
  2758.   mi_vmax = 0;
  2759. }
  2760.  
  2761. /* Subroutines of push_class_decls ().  */
  2762.  
  2763. /* Add the instance variables which this class contributed to the
  2764.    current class binding contour.  When a redefinition occurs,
  2765.    if the redefinition is strictly within a single inheritance path,
  2766.    we just overwrite (in the case of a data field) or
  2767.    cons (in the case of a member function) the old declaration with
  2768.    the new.  If the fields are not within a single inheritance path,
  2769.    we must cons them in either case.  */
  2770.  
  2771. static void
  2772. dfs_pushdecls (type)
  2773.      tree type;
  2774. {
  2775.   tree fields, *methods, *end;
  2776.   tree method_vec;
  2777.  
  2778.   for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
  2779.     {
  2780.       /* Unmark so that if we are in a constructor, and then find that
  2781.      this field was initialized by a base initializer,
  2782.      we can emit an error message.  */
  2783.       if (TREE_CODE (fields) == FIELD_DECL)
  2784.     TREE_USED (fields) = 0;
  2785.  
  2786.       if (DECL_ANON_UNION_ELEM (fields))
  2787.     {
  2788.       dfs_pushdecls (TREE_TYPE (fields));
  2789.       continue;
  2790.     }
  2791.       if (TREE_CODE (fields) != TYPE_DECL)
  2792.     {
  2793.       TREE_FIELD_PUBLIC (fields) = 0;
  2794.       TREE_FIELD_PROTECTED (fields) = 0;
  2795.       TREE_FIELD_PRIVATE (fields) = 0;
  2796.     }
  2797.  
  2798.       if (DECL_NAME (fields))
  2799.     {
  2800.       tree value = IDENTIFIER_CLASS_VALUE (DECL_NAME (fields));
  2801.       if (value)
  2802.         {
  2803.           /* Possible ambiguity.  If its defining type(s)
  2804.          is (are all) derived from us, no problem.  */
  2805.  
  2806.           if (TREE_CODE (value) != TREE_LIST)
  2807.         {
  2808.           if (DECL_FIELD_CONTEXT (value) == type
  2809.               || DERIVES_FROM (DECL_FIELD_CONTEXT (value), type))
  2810.             value = fields;
  2811.           else
  2812.             value = tree_cons (NULL_TREE, fields,
  2813.                        build_tree_list (NULL_TREE, value));
  2814.         }
  2815.           else
  2816.         {
  2817.           /* All children may derive from us, in which case
  2818.              there is no problem.  Otherwise, we have to
  2819.              keep lists around of what the ambiguities might be.  */
  2820.           tree values;
  2821.           int problem = 0;
  2822.  
  2823.           for (values = value; values; values = TREE_CHAIN (values))
  2824.             {
  2825.               tree sub_values = TREE_VALUE (values);
  2826.               if (TREE_CODE (sub_values) == TREE_LIST)
  2827.             {
  2828.               for (; sub_values; sub_values = TREE_CHAIN (sub_values))
  2829.                 if (! DERIVES_FROM (DECL_FIELD_CONTEXT (TREE_VALUE (sub_values)), type))
  2830.                   {
  2831.                 value = tree_cons (NULL_TREE, TREE_VALUE (values), value);
  2832.                 problem = 1;
  2833.                 break;
  2834.                   }
  2835.             }
  2836.               else
  2837.             {
  2838.               if (! DERIVES_FROM (DECL_FIELD_CONTEXT (sub_values), type))
  2839.                 {
  2840.                   value = tree_cons (NULL_TREE, values, value);
  2841.                   problem = 1;
  2842.                   break;
  2843.                 }
  2844.             }
  2845.             }
  2846.           if (! problem) value = fields;
  2847.         }
  2848.  
  2849.           /* Mark this as a potentially ambiguous member.  */
  2850.           if (TREE_CODE (value) == TREE_LIST)
  2851.         {
  2852.           /* Leaving TREE_TYPE blank is intentional.
  2853.              We cannot use `error_mark_node' (lookup_name)
  2854.              or `unknown_type_node' (all member functions use this).  */
  2855.           TREE_NONLOCAL (value) = 1;
  2856.         }
  2857.  
  2858.           IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = value;
  2859.         }
  2860.       else IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = fields;
  2861.     }
  2862.     }
  2863.  
  2864.   method_vec = CLASSTYPE_METHOD_VEC (type);
  2865.   if (method_vec != 0)
  2866.     {
  2867.       /* Farm out constructors and destructors.  */
  2868.       methods = &TREE_VEC_ELT (method_vec, 1);
  2869.       end = TREE_VEC_END (method_vec);
  2870.  
  2871.       /* This does not work for multiple inheritance yet.  */
  2872.       while (methods != end)
  2873.     {
  2874.       /* This will cause lookup_name to return a pointer
  2875.          to the tree_list of possible methods of this name.
  2876.          If the order is a problem, we can nreverse them.  */
  2877.       tree tmp;
  2878.       tree old = IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods));
  2879.  
  2880.       if (old && TREE_CODE (old) == TREE_LIST)
  2881.         tmp = tree_cons (DECL_ORIGINAL_NAME (*methods), *methods, old);
  2882.       else
  2883.         {
  2884.           /* Only complain if we shadow something we can access.  */
  2885.           if (old && (DECL_CONTEXT (old) == current_class_type
  2886.               || ! TREE_PRIVATE (old)))
  2887.         /* Should figure out visibility more accurately.  */
  2888.         warning ("shadowing member `%s' with member function",
  2889.              IDENTIFIER_POINTER (DECL_ORIGINAL_NAME (*methods)));
  2890.           tmp = build_tree_list (DECL_ORIGINAL_NAME (*methods), *methods);
  2891.         }
  2892.  
  2893.       TREE_TYPE (tmp) = unknown_type_node;
  2894. #if 0
  2895.       TREE_OVERLOADED (tmp) = DECL_OVERLOADED (*methods);
  2896. #endif
  2897.       TREE_NONLOCAL (tmp) = 1;
  2898.       IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods)) = tmp;
  2899.  
  2900.       tmp = *methods;
  2901.       while (tmp != 0)
  2902.         {
  2903.           TREE_FIELD_PUBLIC (tmp) = 0;
  2904.           TREE_FIELD_PROTECTED (tmp) = 0;
  2905.           TREE_FIELD_PRIVATE (tmp) = 0;
  2906.           tmp = TREE_CHAIN (tmp);
  2907.         }
  2908.  
  2909.       methods++;
  2910.     }
  2911.     }
  2912.   CLASSTYPE_MARKED (type) = 1;
  2913. }
  2914.  
  2915. /* Consolidate unique (by name) member functions.  */
  2916. static void
  2917. dfs_compress_decls (type)
  2918.      tree type;
  2919. {
  2920.   tree method_vec = CLASSTYPE_METHOD_VEC (type);
  2921.  
  2922.   if (method_vec != 0)
  2923.     {
  2924.       /* Farm out constructors and destructors.  */
  2925.       tree *methods = &TREE_VEC_ELT (method_vec, 1);
  2926.       tree *end = TREE_VEC_END (method_vec);
  2927.  
  2928.       for (; methods != end; methods++)
  2929.     {
  2930.       tree tmp = IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods));
  2931.  
  2932.       /* This was replaced in scope by somebody else.  Just leave it
  2933.          alone.  */
  2934.       if (TREE_CODE (tmp) != TREE_LIST)
  2935.         continue;
  2936.  
  2937.       if (TREE_CHAIN (tmp) == NULL_TREE
  2938.           && TREE_VALUE (tmp)
  2939.           && TREE_CHAIN (TREE_VALUE (tmp)) == NULL_TREE)
  2940.         {
  2941.           IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods))
  2942.         = TREE_VALUE (tmp);
  2943.         }
  2944.     }
  2945.     }
  2946.   CLASSTYPE_MARKED (type) = 0;
  2947. }
  2948.  
  2949. /* When entering the scope of a class, we cache all of the
  2950.    fields that that class provides within its inheritance
  2951.    lattice.  Where ambiguities result, we mark them
  2952.    with `error_mark_node' so that if they are encountered
  2953.    without explicit qualification, we can emit an error
  2954.    message.  */
  2955. void
  2956. push_class_decls (type)
  2957.      tree type;
  2958. {
  2959.   struct obstack *ambient_obstack = current_obstack;
  2960.  
  2961. #if 0
  2962.   tree tags = CLASSTYPE_TAGS (type);
  2963.  
  2964.   while (tags)
  2965.     {
  2966.       tree code_type_node;
  2967.       tree tag;
  2968.  
  2969.       switch (TREE_CODE (TREE_VALUE (tags)))
  2970.     {
  2971.     case ENUMERAL_TYPE:
  2972.       code_type_node = enum_type_node;
  2973.       break;
  2974.     case RECORD_TYPE:
  2975.       code_type_node = record_type_node;
  2976.       break;
  2977.     case CLASS_TYPE:
  2978.       code_type_node = class_type_node;
  2979.       break;
  2980.     case UNION_TYPE:
  2981.       code_type_node = union_type_node;
  2982.       break;
  2983.     default:
  2984.       assert (0);
  2985.     }
  2986.       tag = xref_tag (code_type_node, TREE_PURPOSE (tags),
  2987.               CLASSTYPE_BASECLASS (TREE_VALUE (tags), 1));
  2988.       pushdecl (build_decl (TYPE_DECL, TREE_PURPOSE (tags), TREE_VALUE (tags)));
  2989.     }
  2990. #endif
  2991.  
  2992.   current_obstack = &bridge_obstack;
  2993.   search_stack = push_search_level (search_stack, &bridge_obstack);
  2994.  
  2995.   /* Push class fields into CLASS_VALUE scope, and mark.  */
  2996.   dfs_walk (type, dfs_pushdecls, unmarkedp);
  2997.  
  2998.   /* Compress fields which have only a single entry
  2999.      by a given name, and unmark.  */
  3000.   dfs_walk (type, dfs_compress_decls, markedp);
  3001.   current_obstack = ambient_obstack;
  3002. }
  3003.  
  3004. static void
  3005. dfs_popdecls (type)
  3006.      tree type;
  3007. {
  3008.   tree fields = TYPE_FIELDS (type);
  3009.   tree method_vec = CLASSTYPE_METHOD_VEC (type);
  3010.  
  3011.   while (fields)
  3012.     {
  3013.       if (DECL_ANON_UNION_ELEM (fields))
  3014.     {
  3015.       dfs_popdecls (TREE_TYPE (fields));
  3016.     }
  3017.       else if (DECL_NAME (fields))
  3018.     IDENTIFIER_CLASS_VALUE (DECL_NAME (fields)) = NULL_TREE;
  3019.       fields = TREE_CHAIN (fields);
  3020.     }
  3021.   if (method_vec != 0)
  3022.     {
  3023.       tree *methods = &TREE_VEC_ELT (method_vec, 0);
  3024.       tree *end = TREE_VEC_END (method_vec);
  3025.  
  3026.       if (*methods == 0)
  3027.     methods += 1;
  3028.  
  3029.       for (; methods != end; methods++)
  3030.     IDENTIFIER_CLASS_VALUE (DECL_ORIGINAL_NAME (*methods)) = NULL_TREE;
  3031.     }
  3032.  
  3033.   CLASSTYPE_MARKED (type) = 1;
  3034. }
  3035.  
  3036. void
  3037. pop_class_decls (type)
  3038.      tree type;
  3039. {
  3040.   /* Clear out the IDENTIFIER_CLASS_VALUE which this
  3041.      class may have occupied, and mark.  */
  3042.   dfs_walk (type, dfs_popdecls, unmarkedp);
  3043.  
  3044.   /* Unmark.  */
  3045.   dfs_walk (type, dfs_unmark, markedp);
  3046.   search_stack = pop_search_level (search_stack);
  3047. }
  3048.  
  3049. /* Given a base type PARENT, and a derived type TYPE, build
  3050.    a name which distinguishes exactly the PARENT member of TYPE's type.
  3051.  
  3052.    FORMAT is a string which controls how sprintf formats the name
  3053.    we have generated.
  3054.  
  3055.    For example, given
  3056.  
  3057.     class A; class B; class C : A, B;
  3058.  
  3059.    it is possible to distinguish "A" from "C's A".  And given
  3060.  
  3061.     class L;
  3062.     class A : L; class B : L; class C : A, B;
  3063.  
  3064.    it is possible to distinguish "L" from "A's L", and also from
  3065.    "C's L from A".  */
  3066. tree
  3067. build_type_pathname (format, parent, type)
  3068.      char *format;
  3069.      tree parent, type;
  3070. {
  3071.   extern struct obstack temporary_obstack;
  3072.   char *first, *base, *name;
  3073.   int i;
  3074.   tree id;
  3075.  
  3076.   parent = TYPE_MAIN_VARIANT (parent);
  3077.  
  3078.   /* Remember where to cut the obstack to.  */
  3079.   first = obstack_base (&temporary_obstack);
  3080.  
  3081.   /* Put on TYPE+PARENT.  */
  3082.   obstack_grow (&temporary_obstack,
  3083.         TYPE_NAME_STRING (type), TYPE_NAME_LENGTH (type));
  3084.   obstack_1grow (&temporary_obstack, JOINER);
  3085.   obstack_grow0 (&temporary_obstack,
  3086.          TYPE_NAME_STRING (parent), TYPE_NAME_LENGTH (parent));
  3087.   i = obstack_object_size (&temporary_obstack);
  3088.   base = obstack_base (&temporary_obstack);
  3089.   obstack_finish (&temporary_obstack);
  3090.  
  3091.   /* Put on FORMAT+TYPE+PARENT.  */
  3092.   obstack_blank (&temporary_obstack, strlen (format) + i + 1);
  3093.   name = obstack_base (&temporary_obstack);
  3094.   sprintf (name, format, base);
  3095.   id = get_identifier (name);
  3096.   obstack_free (&temporary_obstack, first);
  3097.  
  3098.   return id;
  3099. }
  3100.  
  3101. static int
  3102. bfs_unmark_finished_struct (type, i)
  3103.      tree type;
  3104.      int i;
  3105. {
  3106.   type = i == 0 ? type : CLASSTYPE_BASECLASS (type, i);
  3107.   if (CLASSTYPE_MARKED4 (type))
  3108.     {
  3109.       tree assoc, decl, context;
  3110.  
  3111.       if (type == current_class_type)
  3112.     assoc = CLASSTYPE_ASSOC (type);
  3113.       else if (TREE_VIA_VIRTUAL (type))
  3114.     assoc = value_member (TYPE_MAIN_VARIANT (type), CLASSTYPE_VBASECLASSES (current_class_type));
  3115.       else
  3116.     assoc = assoc_value (TYPE_MAIN_VARIANT (type), current_class_type);
  3117.       decl = ASSOC_VTABLE (assoc);
  3118.       context = DECL_CONTEXT (decl);
  3119.       DECL_CONTEXT (decl) = 0;
  3120.       if (write_virtuals >= 0
  3121.       && DECL_INITIAL (decl) != ASSOC_VIRTUALS (assoc))
  3122.     DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
  3123.                     ASSOC_VIRTUALS (assoc));
  3124.       finish_decl (decl, DECL_INITIAL (decl), NULL_TREE);
  3125.       DECL_CONTEXT (decl) = context;
  3126.     }
  3127.   CLASSTYPE_MARKED3 (type) = 0;
  3128.   CLASSTYPE_MARKED4 (type) = 0;
  3129.   return 0;
  3130. }
  3131.  
  3132. void
  3133. unmark_finished_struct (type)
  3134.      tree type;
  3135. {
  3136.   bfs_unmark_finished_struct (type, 0);
  3137.   breadth_first_search (type, bfs_unmark_finished_struct, bfs_marked3p);
  3138. }
  3139.  
  3140. void
  3141. print_search_statistics ()
  3142. {
  3143. #ifdef GATHER_STATISTICS
  3144.   if (flag_memoize_lookups)
  3145.     {
  3146.       fprintf (stderr, "%d memoized contexts saved\n",
  3147.            n_contexts_saved);
  3148.       fprintf (stderr, "%d local tree nodes made\n", my_tree_node_counter);
  3149.       fprintf (stderr, "%d local hash nodes made\n", my_memoized_entry_counter);
  3150.       fprintf (stderr, "fields statistics:\n");
  3151.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3152.            memoized_fast_finds[0], memoized_fast_rejects[0],
  3153.            memoized_fields_searched[0]);
  3154.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[0]);
  3155.       fprintf (stderr, "fnfields statistics:\n");
  3156.       fprintf (stderr, "  memoized finds = %d; rejects = %d; (searches = %d)\n",
  3157.            memoized_fast_finds[1], memoized_fast_rejects[1],
  3158.            memoized_fields_searched[1]);
  3159.       fprintf (stderr, "  memoized_adds = %d\n", memoized_adds[1]);
  3160.     }
  3161.   fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
  3162.        n_fields_searched, n_calls_lookup_field, n_calls_lookup_field_1);
  3163.   fprintf (stderr, "%d fnfields searched in %d calls to lookup_fnfields\n",
  3164.        n_outer_fields_searched, n_calls_lookup_fnfields);
  3165.   fprintf (stderr, "%d calls to get_base_type\n", n_calls_get_base_type);
  3166. #else
  3167.   fprintf (stderr, "no search statistics\n");
  3168. #endif
  3169. }
  3170.  
  3171. void
  3172. init_search_processing ()
  3173. {
  3174.   obstack_init (&search_obstack);
  3175.   obstack_init (&type_obstack);
  3176.   obstack_init (&type_obstack_entries);
  3177.   obstack_init (&bridge_obstack);
  3178.  
  3179.   /* This gives us room to build our chains of basetypes,
  3180.      whether or not we decide to memoize them.  */
  3181.   type_stack = push_type_level (0, &type_obstack);
  3182.   _vptr_name = get_identifier ("_vptr");
  3183. }
  3184.  
  3185. tree
  3186. get_wrapper (type)
  3187.      tree type;
  3188. {
  3189.   tree wrap_type;
  3190.   char *name;
  3191.   assert (IS_AGGR_TYPE (type));
  3192.   wrap_type = TYPE_WRAP_TYPE (type);
  3193.   name = (char *)alloca (TYPE_NAME_LENGTH (wrap_type)
  3194.              + strlen (WRAPPER_NAME_FORMAT));
  3195.   sprintf (name, WRAPPER_NAME_FORMAT, TYPE_NAME_STRING (wrap_type));
  3196.   return lookup_fnfields (CLASSTYPE_AS_LIST (wrap_type),
  3197.               get_identifier (name), 0);
  3198. }
  3199.  
  3200. void
  3201. reinit_search_statistics ()
  3202. {
  3203.   my_memoized_entry_counter = 0;
  3204.   memoized_fast_finds[0] = 0;
  3205.   memoized_fast_finds[1] = 0;
  3206.   memoized_adds[0] = 0;
  3207.   memoized_adds[1] = 0;
  3208.   memoized_fast_rejects[0] = 0;
  3209.   memoized_fast_rejects[1] = 0;
  3210.   memoized_fields_searched[0] = 0;
  3211.   memoized_fields_searched[1] = 0;
  3212.   n_fields_searched = 0;
  3213.   n_calls_lookup_field = 0, n_calls_lookup_field_1 = 0;
  3214.   n_calls_lookup_fnfields = 0, n_calls_lookup_fnfields_1 = 0;
  3215.   n_calls_get_base_type = 0;
  3216.   n_outer_fields_searched = 0;
  3217.   n_contexts_saved = 0;
  3218. }
  3219.